public override void ViewDidLoad() { base.ViewDidLoad(); var aButton = new UIBarButtonItem(UIBarButtonSystemItem.Add, (s, e) => { //create a new instance of the picker view controller var picker = ELCImagePickerViewController.Create(15); //Provide custom labels for the picker //var picker = ELCImagePickerViewController.Create(4, "Choisir un album", "Choisir Photo", "Retour", "choix Photo"); //picker.InitCustomLabel(); //set the maximum number of images that can be selected //picker.MaximumImagesCount = 15; //setup the handling of completion once the items have been picked or the picker has been cancelled picker.Completion.ContinueWith(t => { //execute any UI code on the UI thread this.BeginInvokeOnMainThread(() => { //dismiss the picker picker.DismissViewController(true, null); if (t.IsCanceled || t.Exception != null) { //cancelled or error } else { //get the selected items var items = t.Result as List <AssetResult>; foreach (AssetResult aItem in items) { mResults.Add(aItem); } TableView.ReloadData(); } }); }); this.PresentViewController(picker, true, null); }); this.NavigationItem.RightBarButtonItem = aButton; }
//Get photos from album/gallery public async Task UsePhotoGalleryAsync() { var picker = ELCImagePickerViewController.Create(15); SelectedImages = new ObservableCollection <DeliveryImage>(); picker.MaximumImagesCount = 15; // TODO: Have an alert if picked pictures are more that picker.MaximumImagesCount 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) { // User canceled selection action return; } else { foreach (var item in t.Result) { var path = ImageHelper.GetPathToImage(item.Image, item.Name); //TODO: make this it's own function since camera action also needs to do this var imageBytes = ImageHelper.ImageToBinary(path); var newImage = ImageSource.FromStream(() => new MemoryStream(imageBytes)); SelectedImages.Add(new DeliveryImage { ImagePath = path, Source = newImage, OrgImage = imageBytes }); CleanPath(path); } //Send images back MessagingCenter.Send((App)Xamarin.Forms.Application.Current, "SelectedImages", SelectedImages); } }); }); }
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); } }); }); }
//int? IGallery.ImagesSelection //{ // get // { // return Convert.ToInt32(Xamarin.Forms.Application.Current.Properties["ImagesSelection"]); // } // set // { // Xamarin.Forms.Application.Current.Properties["ImagesSelection"] = value; // } //} public void OpenGallery(int imageselection) { try { var picker = ELCImagePickerViewController.Create(imageselection, "Select Images"); int Totalsize = 0; bool IsValid = true; picker.Completion.ContinueWith(t => { picker.BeginInvokeOnMainThread(() => { lstBytes = new List <byte[]>(); _imagename = new List <string>(); picker.DismissViewController(true, null); #region if (t.IsCanceled || t.Exception != null) { //cancelled or error } else { //get the selected items var items = t.Result as List <AssetResult>; foreach (AssetResult aItem in items) { string Extension = aItem.Name.Substring(aItem.Name.IndexOf('.') + 1, aItem.Name.Length - aItem.Name.IndexOf('.') - 1); UIImage objUIImage = aItem.Image; NSData imagedata = new NSData(); if (Extension.ToLower() == "png") { imagedata = objUIImage.AsPNG(); } else { imagedata = objUIImage.AsJPEG(); } byte[] bytes = new byte[imagedata.Length]; Totalsize += bytes.Length; if (Totalsize > 10000000) { IsValid = false; MessagingCenter.Send <App, string>((App)Xamarin.Forms.Application.Current, "imagevalidation", "Image Cannot be Greater than 10 MB"); break; } System.Runtime.InteropServices.Marshal.Copy(imagedata.Bytes, bytes, 0, Convert.ToInt32(imagedata.Length)); lstBytes.Add(bytes); _imagename.Add(aItem.Name); } if (IsValid) { if (_imagename.Count > 0) { MessagingCenter.Send <App, List <byte[]> >((App)Xamarin.Forms.Application.Current, "ImagesSelected", lstBytes); MessagingCenter.Send <App, List <string> >((App)Xamarin.Forms.Application.Current, "ImagesSelectedName", _imagename); } else { MessagingCenter.Send <App, string>((App)Xamarin.Forms.Application.Current, "exception", ""); } } } #endregion }); }); var topController = UIApplication.SharedApplication.KeyWindow.RootViewController; while (topController.PresentedViewController != null) { topController = topController.PresentedViewController; } topController.PresentViewController(picker, true, null); } catch (Exception ex) { throw; } }