Monotouch - 带有 iPad 应用的 UIImagePickerController
Posted
技术标签:
【中文标题】Monotouch - 带有 iPad 应用的 UIImagePickerController【英文标题】:Monotouch - UIImagePickerController with an iPad App 【发布时间】:2011-05-08 15:58:59 【问题描述】:我有一个仅限 iPad 的应用程序,我试图让用户从他们的PhotoLibrary
中选择图像,据我所知,我必须在UIPopOverController
中使用UIImagePickerController
。我尝试了许多不同的方法来做到这一点,但我可以得到任何工作。我看过很多代码 sn-ps,但我似乎无法让它们在 Monotouch 下工作。
有人能指出正确的方法吗?非常感谢。
【问题讨论】:
【参考方案1】:我必须调用创建图像选择器的代码以及对从主线程中选择的图像做出反应的代码才能使其工作:
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);
);
【讨论】:
【参考方案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
【讨论】:
以上是关于Monotouch - 带有 iPad 应用的 UIImagePickerController的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Monotouch 在 iPad 故事板应用程序中以编程方式管理视图
Monotouch - 在 monotouch 项目中附加 Sqlite 数据库并部署到 iPad