Exemple #1
0
        /// <summary>
        /// Setups the controller.
        /// </summary>
        /// <param name="mediaDelegate">The media picker delegate.</param>
        /// <param name="sourceType">Media source type.</param>
        /// <param name="mediaType">Media type.</param>
        /// <param name="options">The options.</param>
        /// <returns>The configured media picker controller.</returns>
        private static MediaPickerController SetupController(
            MediaPickerDelegate mediaDelegate,
            UIImagePickerControllerSourceType sourceType,
            string mediaType,
            CameraMediaStorageOptions options = null)
        {
            // Create the media picker
            var picker = new MediaPickerController(mediaDelegate)
            {
                MediaTypes = new[] { mediaType },
                SourceType = sourceType
            };

            // If the image is from camera
            if (sourceType == UIImagePickerControllerSourceType.Camera)
            {
                if ((mediaType == MediaPickerIOS.TypeImage) && (options != null))
                {
                    // Configure the camera
                    picker.CameraDevice      = MediaPickerIOS.GetCameraDevice(options.DefaultCamera);
                    picker.CameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Photo;
                }
            }

            return(picker);
        }
Exemple #2
0
        /////// <summary>
        /////// Select a picture from library.
        /////// </summary>
        /////// <param name="options">The storage options.</param>
        /////// <returns>Task representing the asynchronous operation.</returns>
        ////public Task<MediaFile> SelectPhotoAsync(CameraMediaStorageOptions options)
        ////{
        ////    // If photos are not supported
        ////    if (!this.IsPhotosSupported)
        ////    {
        ////        throw new NotSupportedException();
        ////    }

        ////    // Get the image from pictures
        ////    return this.GetMediaAsync(UIImagePickerControllerSourceType.PhotoLibrary, MediaPickerIOS.TypeImage);
        ////}

        /// <summary>
        /// Takes the picture.
        /// </summary>
        /// <param name="options">The storage options.</param>
        /// <returns>Task representing the asynchronous operation.</returns>
        public override Task <MediaFile> TakePhotoAsync(CameraMediaStorageOptions options)
        {
            // If photos are not supported
            if (!this.IsPhotosSupported)
            {
                throw new NotSupportedException();
            }

            // If camera is not supported
            if (!this.IsCameraAvailable)
            {
                throw new NotSupportedException();
            }

            // If the camera permission is not determined
            var status = AVCaptureDevice.GetAuthorizationStatus(AVMediaType.Video);

            if (status == AVAuthorizationStatus.NotDetermined)
            {
                var granted = AVCaptureDevice.RequestAccessForMediaTypeAsync(AVMediaType.Video);
                granted.Wait();
                if (!granted.Result)
                {
                    return(null);
                }
            }

            // If camera is restricted
            if ((status == AVAuthorizationStatus.Denied) || (status == AVAuthorizationStatus.Restricted))
            {
                return(null);
            }

            // Take the camera photo
            MediaPickerIOS.VerifyCameraOptions(options);
            return(this.GetMediaAsync(UIImagePickerControllerSourceType.Camera, MediaPickerIOS.TypeImage, options));
        }
Exemple #3
0
        /// <summary>
        /// Gets the media asynchronous.
        /// </summary>
        /// <param name="sourceType">Type of the source.</param>
        /// <param name="mediaType">Type of the media.</param>
        /// <param name="options">The options.</param>
        /// <returns>Task representing the asynchronous operation.</returns>
        private Task <MediaFile> GetMediaAsync(
            UIImagePickerControllerSourceType sourceType,
            string mediaType,
            CameraMediaStorageOptions options = null)
        {
            // Get the active window
            var window = UIApplication.SharedApplication.KeyWindow;

            if (window == null)
            {
                throw new InvalidOperationException("There's no current active window");
            }

            // Get the view controller
            var viewController = window.RootViewController;

#if __IOS_10__
            if (viewController == null || (viewController.PresentedViewController != null && viewController.PresentedViewController.GetType() == typeof(UIAlertController)))
            {
                window =
                    UIApplication.SharedApplication.Windows.OrderByDescending(w => w.WindowLevel)
                    .FirstOrDefault(w => w.RootViewController != null);

                if (window == null)
                {
                    throw new InvalidOperationException("Could not find current view controller");
                }

                viewController = window.RootViewController;
            }
#endif

            // Get the root view controller
            while (viewController.PresentedViewController != null)
            {
                viewController = viewController.PresentedViewController;
            }

            // Create a new media picker delegate
            var newDelegate     = new MediaPickerDelegate(viewController, sourceType, options);
            var operationResult = Interlocked.CompareExchange(ref this.pickerDelegate, newDelegate, null);
            if (operationResult != null)
            {
                throw new InvalidOperationException("Only one operation can be active at at time");
            }

            // Setup the view controller
            var picker = MediaPickerIOS.SetupController(newDelegate, sourceType, mediaType, options);

            // If the image is from photo library
            if ((UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad) &&
                (sourceType == UIImagePickerControllerSourceType.PhotoLibrary))
            {
                // Create a photo choosing popover
                newDelegate.Popover = new UIPopoverController(picker)
                {
                    Delegate = new MediaPickerPopoverDelegate(newDelegate, picker)
                };
                newDelegate.DisplayPopover();
            }
            else
            {
                // Show the media picker view
                viewController.PresentViewController(picker, true, null);
            }

            // Get the media
            return(newDelegate.Task.ContinueWith(
                       t =>
            {
                // Dispose popover if any
                if (this.popover != null)
                {
                    this.popover.Dispose();
                    this.popover = null;
                }

                // Release the media picker delegate
                Interlocked.Exchange(ref this.pickerDelegate, null);
                return t;
            }).Unwrap());
        }