Monotouch - 带iPad应用程序的UIImagePickerController

时间:2011-05-08 15:58:59

标签: ipad xamarin.ios uiimagepickercontroller uipopovercontroller

我有一个仅限iPad的应用程序,我试图让用户从PhotoLibrary附近选择图片,因为我可以告诉我必须使用UIImagePickerController中的UIPopOverController 。我已经尝试了很多不同的方法来做到这一点,但我可以得到任何工作。我已经看过很多代码片段,但我似乎无法让它们在Monotouch下工作。

有人能以正确的方式指出我吗?我非常感激。

2 个答案:

答案 0 :(得分:3)

我不得不调用创建图像选择器的代码以及对从主线程中拾取的图像作出反应的代码,以使其工作:

    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);                    
        });
    }

答案 1 :(得分:2)

以下是我在应用中使用的代码 - 这应该是您开始工作的良好开端。

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
    }
}