public Task <AttachedFileDataModel> BuildAttachedPictureAsync(Stream pictureStream) => Task <AttachedFileDataModel> .Run(async() => { FileDTO pictureFile = new FileDTO(); pictureFile.Base64 = await _mediaPickerService.ParseStreamToBase64(pictureStream); pictureFile.Name = string.Format("{0}.{1}", Guid.NewGuid(), ProfileMediaService.PNG_IMAGE_FORMAT); FileDTO pictureThumbnail = new FileDTO(); pictureThumbnail.Base64 = pictureFile.Base64; pictureThumbnail.Name = string.Format("{0}_thumbnail.{1}", Guid.NewGuid(), ProfileMediaService.PNG_IMAGE_FORMAT); pictureStream.Dispose(); pictureStream.Close(); return(new AttachedFileDataModel() { File = pictureFile, Thumbnail = pictureThumbnail, MimeType = ProfileMediaService.IMAGE_MEDIA_TYPE }); });
//public Task<IPickedMediaFromGallery> TakePhotoOrVideoAsync() => // Task<IPickedMediaFromGallery>.Run(() => { // PickedMediaFromGallery pickedMediaResult = new PickedMediaFromGallery(); // try { // UIViewController topController = UIApplication.SharedApplication.KeyWindow.RootViewController; // while (topController.PresentedViewController != null) { // topController = topController.PresentedViewController; // } // UIAlertController actionSheet = UIAlertController.Create("Make a choise", "Take Image or Video with:", UIAlertControllerStyle.ActionSheet); // /// // /// Use photo camera action // /// // actionSheet.AddAction(UIAlertAction.Create("Camera", UIAlertActionStyle.Default, (action) => { // if (UIImagePickerController.IsSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)) { // UIImagePickerController imagePicker = new UIImagePickerController(); // imagePicker.SourceType = UIImagePickerControllerSourceType.Camera; // imagePicker.MediaTypes = UIImagePickerController.AvailableMediaTypes(UIImagePickerControllerSourceType.Camera); // imagePicker.FinishedPickingMedia += (sender, args) => { // if (args?.OriginalImage != null) { // /// // /// Figure out how much to scale down by // /// // int inSampleSize = GetInSampleSize(args.OriginalImage.Size.Width, args.OriginalImage.Size.Height); // UIImage originalImage = args.OriginalImage.Scale(new CGSize(args.OriginalImage.Size.Width / inSampleSize, args.OriginalImage.Size.Height / inSampleSize)); // string imageBase64 = originalImage.AsJPEG().GetBase64EncodedString(NSDataBase64EncodingOptions.EndLineWithLineFeed); // pickedMediaResult.Completion = true; // pickedMediaResult.DataBase64 = imageBase64; // pickedMediaResult.DataThumbnailBase64 = imageBase64; // pickedMediaResult.MimeType = ProfileMediaService.IMAGE_MEDIA_TYPE; // } // topController.DismissModalViewController(true); // }; // imagePicker.Canceled += (sender, args) => { // topController.DismissModalViewController(true); // pickedMediaResult.Completion = true; // }; // topController.PresentModalViewController(imagePicker, true); // } // else { // UIAlertController alert = UIAlertController.Create("Warning", "Your device don't have camera", UIAlertControllerStyle.Alert); // alert.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, (alertAction) => { // pickedMediaResult.Completion = false; // pickedMediaResult.ErrorMessage = _TAKE_IMAGE_COMMON_ERROR; // })); // topController.PresentViewController(alert, true, null); // } // })); // /// // /// Use photo camera action // /// // //actionSheet.AddAction(UIAlertAction.Create("Video camera", UIAlertActionStyle.Default, (action) => { // // if (UIVide.IsSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)) { // // UIImagePickerController imagePicker = new UIImagePickerController(); // // imagePicker.SourceType = UIImagePickerControllerSourceType.Camera; // // imagePicker.MediaTypes = UIImagePickerController.AvailableMediaTypes(UIImagePickerControllerSourceType.Camera); // // imagePicker.FinishedPickingMedia += (sender, args) => { // // if (args?.OriginalImage != null) { // // /// // // /// Figure out how much to scale down by // // /// // // int inSampleSize = GetInSampleSize(args.OriginalImage.Size.Width, args.OriginalImage.Size.Height); // // UIImage originalImage = args.OriginalImage.Scale(new CGSize(args.OriginalImage.Size.Width / inSampleSize, args.OriginalImage.Size.Height / inSampleSize)); // // string imageBase64 = originalImage.AsJPEG().GetBase64EncodedString(NSDataBase64EncodingOptions.EndLineWithLineFeed); // // pickedMediaResult.Completion = true; // // pickedMediaResult.DataBase64 = imageBase64; // // pickedMediaResult.DataThumbnailBase64 = imageBase64; // // pickedMediaResult.MimeType = ProfileMediaService.IMAGE_MEDIA_TYPE; // // } // // topController.DismissModalViewController(true); // // }; // // imagePicker.Canceled += (sender, args) => { // // topController.DismissModalViewController(true); // // pickedMediaResult.Completion = true; // // }; // // topController.PresentModalViewController(imagePicker, true); // // } // // else { // // UIAlertController alert = UIAlertController.Create("Warning", "Your device don't have camera", UIAlertControllerStyle.Alert); // // alert.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, (alertAction) => { // // pickedMediaResult.Completion = false; // // pickedMediaResult.ErrorMessage = _TAKE_IMAGE_COMMON_ERROR; // // })); // // topController.PresentViewController(alert, true, null); // // } // //})); // } // catch (Exception exc) { // pickedMediaResult.Completion = false; // pickedMediaResult.Exception = exc; // pickedMediaResult.ErrorMessage = _TAKE_MEDIA_COMMON_ERROR; // } // return (IPickedMediaFromGallery)pickedMediaResult; // }); public Task <IPickedMediaFromGallery> TakePhotoOrVideoAsync() { _takeVideoOrImageTaskCompletion = new TaskCompletionSource <IPickedMediaFromGallery>(); try { UIViewController topController = UIApplication.SharedApplication.KeyWindow.RootViewController; while (topController.PresentedViewController != null) { topController = topController.PresentedViewController; } IMediaPickerService mediaPickerService = ViewModelLocator.Resolve <IMediaPickerService>(); UIAlertController actionSheet = UIAlertController.Create("Make a choise", "Take Image or Video with:", UIAlertControllerStyle.ActionSheet); /// /// Use photo camera action /// actionSheet.AddAction(UIAlertAction.Create("Camera", UIAlertActionStyle.Default, async(action) => { if (UIImagePickerController.IsSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)) { try { MediaFile mediaFile = await mediaPickerService.TakePhotoAsync(); if (mediaFile != null) { string base64 = await mediaPickerService.ParseStreamToBase64(mediaFile.GetStream()); _takeVideoOrImageTaskCompletion.SetResult(new PickedMediaFromGallery() { Completion = true, DataBase64 = base64, MimeType = ProfileMediaService.IMAGE_MEDIA_TYPE, DataThumbnailBase64 = base64 }); mediaFile.Dispose(); } else { _takeVideoOrImageTaskCompletion.SetResult(new PickedMediaFromGallery() { Completion = true }); } } catch (Exception exc) { _takeVideoOrImageTaskCompletion.SetResult(new PickedMediaFromGallery() { ErrorMessage = _TAKE_IMAGE_COMMON_ERROR, Exception = exc }); } topController.DismissModalViewController(true); } else { UIAlertController alert = UIAlertController.Create("Warning", "Your device don't have camera", UIAlertControllerStyle.Alert); alert.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, (alertAction) => _takeVideoOrImageTaskCompletion.SetResult(new PickedMediaFromGallery() { ErrorMessage = _TAKE_IMAGE_COMMON_ERROR }))); topController.PresentViewController(alert, true, null); } })); /// /// Use photo camera action /// actionSheet.AddAction(UIAlertAction.Create("Video camera", UIAlertActionStyle.Default, async(action) => { if (UIImagePickerController.IsSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)) { try { MediaFile mediaFile = await mediaPickerService.TakeVideoAsync(); if (mediaFile != null) { string thumbnailBase64 = await mediaPickerService.ParseStreamToBase64(GetThumbnail(mediaFile.Path)); string base64 = await mediaPickerService.ParseStreamToBase64(mediaFile.GetStream()); _takeVideoOrImageTaskCompletion.SetResult(new PickedMediaFromGallery() { Completion = true, DataBase64 = base64, MimeType = ProfileMediaService.VIDEO_MEDIA_TYPE, DataThumbnailBase64 = thumbnailBase64 }); mediaFile.Dispose(); } else { _takeVideoOrImageTaskCompletion.SetResult(new PickedMediaFromGallery() { Completion = true }); } } catch (Exception exc) { _takeVideoOrImageTaskCompletion.SetResult(new PickedMediaFromGallery() { ErrorMessage = _TAKE_IMAGE_COMMON_ERROR, Exception = exc }); } topController.DismissModalViewController(true); } else { UIAlertController alert = UIAlertController.Create("Warning", "Your device don't have camera", UIAlertControllerStyle.Alert); alert.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, (alertAction) => _takeVideoOrImageTaskCompletion.SetResult(new PickedMediaFromGallery() { ErrorMessage = _TAKE_IMAGE_COMMON_ERROR }))); topController.PresentViewController(alert, true, null); } })); topController.PresentViewController(actionSheet, true, null); } catch (Exception exc) { _takeVideoOrImageTaskCompletion.SetResult(new PickedMediaFromGallery() { Exception = exc, ErrorMessage = _TAKE_VIDEO_COMMON_ERROR }); } return(_takeVideoOrImageTaskCompletion.Task); }