What I want to do is create a Popup with a UIImagePickerController. This parts easy but I want to create a utility method that generates the UIImagePickerController popup and returns the UIImage once the user selects it. The problem is that the UIImagePickerController has a delegate property that is used for asynchronous completion. My thought was that maybe I could pass in a delegate to my utility function that contains the code to execute once the image is selected but the code to execute needs to operate on the image that was selected. This is the code I have so far and just so everyone knows, it crashes. I believe it’s because I’m executing it in a static method.
namespace GalleryProto
{
static public class CameraUtility
{
public static void GetImageFromGalleryWithPopup(UIViewController parentViewController, PointF centerPoint)
{
UIImagePickerController imagePicker;
UIPopoverController popOver;
imagePicker = new UIImagePickerController ();
popOver = new UIPopoverController (imagePicker);
popOver.DidDismiss += (popOverController, e) =>
{
if (popOver != null && popOver.PopoverVisible) {
Console.WriteLine ("Popover Dismissed.");
popOver.Dismiss (true);
imagePicker.Dispose ();
popOver.Dispose ();
imagePicker = null;
popOver = null;
}
};
Console.WriteLine ("Before Finished Picking Image Delegate.");
imagePicker.Delegate = new MyPickerDelegate (imagePicker, popOver);
imagePicker.SourceType = UIImagePickerControllerSourceType.PhotoLibrary;
imagePicker.AllowsEditing = false;
imagePicker.MediaTypes = new string[] {"public.image"};
RectangleF popRectangle = new RectangleF (centerPoint, new SizeF (1, 1));
popOver.PresentFromRect (popRectangle, parentViewController.View, 0, true); //Center the popup on the Image Content View.
}
public class MyPickerDelegate : UIImagePickerControllerDelegate
{
UIImagePickerController _imagePicker;
UIPopoverController _popOver;
public MyPickerDelegate(UIImagePickerController imagePicker, UIPopoverController popOver)
{
_imagePicker = imagePicker;
_popOver = popOver;
}
public override void Canceled (UIImagePickerController picker)
{
Console.WriteLine("Canceleled");
}
public override void FinishedPickingImage (UIImagePickerController picker, UIImage image, NSDictionary editingInfo)
{
Console.WriteLine("Finished Picking Image");
_popOver.Dismiss (true);
_imagePicker.Dispose ();
_popOver.Dispose ();
_imagePicker = null;
_popOver = null;
}
}
}
}
I solved my issue by passing in a delegate for the callback that takes a UIImage as an argument so that the image can be manipulated appropriately once it’s selected.