예제 #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&lt;MediaFile&gt;.</returns>
		/// <exception cref="InvalidOperationException">
		/// There's no current active window
		/// or
		/// Could not find current view controller
		/// or
		/// Only one operation can be active at at time
		/// </exception>
		private Task<MediaFile> GetMediaAsync(
			UIImagePickerControllerSourceType sourceType,
			string mediaType,
			MediaStorageOptions options = null)
		{
			var window = UIApplication.SharedApplication.KeyWindow;
			if (window == null)
			{
				throw new InvalidOperationException("There's no current active window");
			}

			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 
			while (viewController.PresentedViewController != null)
			{
				viewController = viewController.PresentedViewController;
			}

			var ndelegate = new MediaPickerDelegate(viewController, sourceType, options);
			var od = Interlocked.CompareExchange(ref _pickerDelegate, ndelegate, null);
			if (od != null)
			{
				throw new InvalidOperationException("Only one operation can be active at at time");
			}

			var picker = SetupController(ndelegate, sourceType, mediaType, options);

			if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad
			    && sourceType == UIImagePickerControllerSourceType.PhotoLibrary)
			{
			    ndelegate.Popover = new UIPopoverController(picker)
			    {
			        Delegate = new MediaPickerPopoverDelegate(ndelegate, picker)
			    };
			    ndelegate.DisplayPopover();
			}
			else
			{
				viewController.PresentViewController(picker, true, null);
			}

			return ndelegate.Task.ContinueWith(
				t =>
					{
						if (_popover != null)
						{
							_popover.Dispose();
							_popover = null;
						}

						Interlocked.Exchange(ref _pickerDelegate, null);
						return t;
					}).Unwrap();
		}
예제 #2
0
		/// <summary>
		/// Setups the controller.
		/// </summary>
		/// <param name="mpDelegate">The mp delegate.</param>
		/// <param name="sourceType">Type of the source.</param>
		/// <param name="mediaType">Type of the media.</param>
		/// <param name="options">The options.</param>
		/// <returns>MediaPickerController.</returns>
		private static MediaPickerController SetupController(
			MediaPickerDelegate mpDelegate,
			UIImagePickerControllerSourceType sourceType,
			string mediaType,
			MediaStorageOptions options = null)
		{
		    var picker = new MediaPickerController(mpDelegate) { MediaTypes = new[] { mediaType }, SourceType = sourceType};

		    if (sourceType == UIImagePickerControllerSourceType.Camera)
			{
                if (mediaType == TypeImage && options is CameraMediaStorageOptions)
				{
					picker.CameraDevice = GetCameraDevice(((CameraMediaStorageOptions)options).DefaultCamera);
					picker.CameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Photo;
				}
				else if (mediaType == TypeMovie && options is VideoMediaStorageOptions)
				{
					var voptions = (VideoMediaStorageOptions)options;

					picker.CameraDevice = GetCameraDevice (voptions.DefaultCamera);
					picker.CameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Video;
					picker.VideoQuality = GetQuailty(voptions.Quality);
					picker.VideoMaximumDuration = voptions.DesiredLength.TotalSeconds;
				}
			}

			return picker;
		}
 /// <summary>
 /// Initializes a new instance of the <see cref="MediaPickerController"/> class.
 /// </summary>
 /// <param name="mpDelegate">The mp delegate.</param>
 internal MediaPickerController(MediaPickerDelegate mpDelegate)
 {
     base.Delegate = mpDelegate;
 }
예제 #4
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&lt;MediaFile&gt;.</returns>
        /// <exception cref="InvalidOperationException">
        /// There's no current active window
        /// or
        /// Could not find current view controller
        /// or
        /// Only one operation can be active at at time
        /// </exception>
        private Task <MediaFile> GetMediaAsync(
            UIImagePickerControllerSourceType sourceType,
            string mediaType,
            MediaStorageOptions options = null)
        {
            var window = UIApplication.SharedApplication.KeyWindow;

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

            var viewController = window.RootViewController;

            if (viewController == null)
            {
                window         = UIApplication.SharedApplication.Windows.OrderByDescending(w => w.WindowLevel).FirstOrDefault(w => w.RootViewController != null);
                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
            while (viewController.PresentedViewController != null)
            {
                viewController = viewController.PresentedViewController;
            }

            var ndelegate = new MediaPickerDelegate(viewController, sourceType, options);
            //var od = Interlocked.CompareExchange(ref _pickerDelegate, ndelegate, null);
            //if (od != null)
            //{
            //	throw new InvalidOperationException("Only one operation can be active at at time");
            //}
            _pickerDelegate = ndelegate;

            var picker = SetupController(ndelegate, sourceType, mediaType, options);

            if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad &&
                sourceType == UIImagePickerControllerSourceType.PhotoLibrary)
            {
                ndelegate.Popover = new UIPopoverController(picker)
                {
                    Delegate = new MediaPickerPopoverDelegate(ndelegate, picker)
                };
                ndelegate.DisplayPopover();
            }
            else
            {
                viewController.PresentViewController(picker, true, null);
            }

            return(ndelegate.Task.ContinueWith(
                       t =>
            {
                if (_popover != null)
                {
                    _popover.Dispose();
                    _popover = null;
                }

                //Interlocked.Exchange(ref _pickerDelegate, null);
                _pickerDelegate = null;
                return t;
            }).Unwrap());
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="MediaPickerPopoverDelegate"/> class.
 /// </summary>
 /// <param name="pickerDelegate">The picker delegate.</param>
 /// <param name="picker">The picker.</param>
 internal MediaPickerPopoverDelegate(MediaPickerDelegate pickerDelegate, UIImagePickerController picker)
 {
     _pickerDelegate = pickerDelegate;
     _picker         = picker;
 }
		/// <summary>
		/// Initializes a new instance of the <see cref="MediaPickerController"/> class.
		/// </summary>
		/// <param name="mpDelegate">The mp delegate.</param>
		internal MediaPickerController(MediaPickerDelegate mpDelegate)
		{
			base.Delegate = mpDelegate;
		}
		/// <summary>
		/// Initializes a new instance of the <see cref="MediaPickerPopoverDelegate"/> class.
		/// </summary>
		/// <param name="pickerDelegate">The picker delegate.</param>
		/// <param name="picker">The picker.</param>
		internal MediaPickerPopoverDelegate(MediaPickerDelegate pickerDelegate, UIImagePickerController picker)
		{
			_pickerDelegate = pickerDelegate;
			_picker = picker;
		}