I have an iPad only application that I'm trying to allow users to select images from their PhotoLibrary
, near as I can tell I have to use the UIImagePickerController
in a UIPopOverController
. I have attempted many different ways to do this but I can get anything to work. I've seen a lot of code snippets but I can't seem to get them working under Monotouch.
Could somebody point me at the correct way to do this? I greatly appreciate it.
I had to call the code that creates the image picker and the code that reacts to the image being picked from the main thread to get it to 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);
});
}
Here is code that I've used in an app - this should serve as a good start for you to get it working.
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With