Monotouch - UIImagePickerController with iPad Application

I have an iPad app that I am trying to allow users to select images from their PhotoLibrary , nearby, as I can say that I have to use the UIImagePickerController in the UIPopOverController . I tried to do many different ways, but I can make everything work. I saw a lot of code snippets, but I can't get them to work in Monotouch.

Can someone tell me the correct way to do this? I am very grateful.

+6
source share
2 answers

I had to call the code that creates the image picker, and the code that responds to the image selected from the main thread to make it work:

  partial void OnImport (UIButton s) { BeginInvokeOnMainThread(delegate { UIImagePickerController picker = new UIImagePickerController(); picker.ContentSizeForViewInPopover = new System.Drawing.SizeF(320,480); UIPopoverController popover = new UIPopoverController(picker); picker.FinishedPickingImage += delegate(object sender, UIImagePickerImagePickedEventArgs e) { BeginInvokeOnMainThread(delegate { UIImage image = (UIImage)info.ObjectForKey(new NSString("UIImagePickerControllerOriginalImage")); picker.DismissModalViewControllerAnimated(true); // do something with image }); }; picker.SourceType = UIImagePickerControllerSourceType.PhotoLibrary; popover.PresentFromRect(s.Frame, this.View, UIPopoverArrowDirection.Left, true); }); } 
+3
source

Here is the code I used in the application - this should be a good start for you to make it work.

 UIImagePickerController imagePicker; UIPopoverController popOver; void AttachImageBtnTouched(object sender, EventArgs e) { if (popOver == null || popOver.ContentViewController == null) { imagePicker = new UIImagePickerController(); popOver = new UIPopoverController(imagePicker); ImagePickerDelegate imgDel = new ImagePickerDelegate(); imagePicker.Delegate = imgDel; imagePicker.SourceType = UIImagePickerControllerSourceType.PhotoLibrary; } if (popOver.PopoverVisible) { popOver.Dismiss(true); imagePicker.Dispose(); popOver.Dispose(); return; } else { popOver.PresentFromRect(btnAttachment.Frame, this.View, UIPopoverArrowDirection.Any, true); } } // The Delegate class looks something like public class ImagePickerDelegate : UIImagePickerControllerDelegate { public ImagePickerDelegate() {} public override void FinishedPickingMedia(UIImagePickerController picker, NSDictionary info) { UIImage image = (UIImage)info.ObjectForKey(new NSString("UIImagePickerControllerOriginalImage")); // do whatever else you'd like to with the image } } 
+2
source

Source: https://habr.com/ru/post/887563/


All Articles