Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monotouch - UIImagePickerController with an iPad App

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.

like image 717
RSH Avatar asked May 08 '11 15:05

RSH


2 Answers

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);                    
        });
    }
like image 130
Nikolai Sander Avatar answered Nov 18 '22 01:11

Nikolai Sander


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
    }
}
like image 2
Luke Avatar answered Nov 18 '22 02:11

Luke