Ejemplo n.º 1
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());
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="MediaPickerController"/> class.
 /// </summary>
 /// <param name="mediaDelegate">The media picker delegate.</param>
 internal MediaPickerController(MediaPickerDelegate mediaDelegate)
 {
     base.Delegate = mediaDelegate;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="MediaPickerPopoverDelegate"/> class.
 /// </summary>
 /// <param name="pickerDelegate">The media picker delegate.</param>
 /// <param name="picker">The media picker controller.</param>
 internal MediaPickerPopoverDelegate(MediaPickerDelegate pickerDelegate, UIImagePickerController picker)
 {
     this.pickerDelegate = pickerDelegate;
     this.picker         = picker;
 }