Created
June 15, 2017 02:30
-
-
Save Char0394/4e39bcd70caef80e946b4002fe7613c5 to your computer and use it in GitHub Desktop.
iOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [assembly: Xamarin.Forms.Dependency(typeof(MediaService))] | |
| namespace SelectMultiIpleImagesApp.iOS | |
| { | |
| public class MediaService : IMediaService | |
| { | |
| public async Task OpenGallery() | |
| { | |
| var picker = ELCImagePickerViewController.Create(15); | |
| picker.MaximumImagesCount = 15; | |
| var topController = UIApplication.SharedApplication.KeyWindow.RootViewController; | |
| while (topController.PresentedViewController != null) | |
| { | |
| topController = topController.PresentedViewController; | |
| } | |
| topController.PresentViewController(picker, true, null); | |
| await picker.Completion.ContinueWith(t => | |
| { | |
| picker.BeginInvokeOnMainThread(() => | |
| { | |
| //dismiss the picker | |
| picker.DismissViewController(true, null); | |
| if (t.IsCanceled || t.Exception != null) | |
| { | |
| } | |
| else | |
| { | |
| List<string> images = new List<string>(); | |
| var items = t.Result as List<AssetResult>; | |
| foreach (var item in items) | |
| { | |
| var path = Save(item.Image, item.Name); | |
| images.Add(path); | |
| //CleanPath(path); | |
| } | |
| MessagingCenter.Send<App, List<string>>((App)Xamarin.Forms.Application.Current, "ImagesSelected", images); | |
| } | |
| }); | |
| }); | |
| } | |
| string Save(UIImage image, string name) | |
| { | |
| var documentsDirectory = Environment.GetFolderPath | |
| (Environment.SpecialFolder.Personal); | |
| string jpgFilename = System.IO.Path.Combine(documentsDirectory, name); // hardcoded filename, overwritten each time | |
| NSData imgData = image.AsJPEG(); | |
| NSError err = null; | |
| if (imgData.Save(jpgFilename, false, out err)) | |
| { | |
| return jpgFilename; | |
| } | |
| else | |
| { | |
| Console.WriteLine("NOT saved as " + jpgFilename + " because" + err.LocalizedDescription); | |
| return null; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment