UIImagePickerController虽然可以用在IPAD与IPhone,但代码有所不同。
IPhone的代码如下:
1: partial void SelectImage(NSObject sender, MonoTouch.UIKit.UIEvent @event) {
2: UIImagePickerController picker = new UIImagePickerController ();
3: picker.ModalPresentationStyle = UIModalPresentationStyle.FullScreen;
4: picker.Delegate = new MyImagePickerDelegate (this);
5: this.PresentModalViewController (picker,true);
6: }
7:
8: public class MyImagePickerDelegate : UIImagePickerControllerDelegate
9: {
10: MYTableViewController _mvc;
11: public MyImagePickerDelegate (MYTableViewController mvc)
12: {
13: _mvc = mvc;
14: }
15: public override void FinishedPickingImage(UIImagePickerController picker, UIImage image, NSDictionary editingInfo) {
16: // TODO: Implement - see: http://go-mono.com/docs/index.aspx?link=T%3aMonoTouch.Foundation.ModelAttribute
17: }
18:
19: public override void Canceled(UIImagePickerController picker) {
20: // TODO: Implement - see: http://go-mono.com/docs/index.aspx?link=T%3aMonoTouch.Foundation.ModelAttribute
21: }
22: }
这段代码在IPhone下是正常的,但在IPAD上会报如下异常
MonoTouch.Foundation.MonoTouchException: Objective-C exception thrown. Name: NSInvalidArgumentException Reason: On iPad, UIImagePickerController must be presented via UIPopoverController
为什么呢?就和异常提的一样。UIImagePickerController必须由UIPopoverController来承载。
IPAD代码:
1: //static UIImagePickerController imagePicker = new UIImagePickerController();
2: //static UIPopoverController popOver = new UIPopoverController (imagePicker);
UIImagePickerController imagePicker;
UIPopoverController popOver;3: partial void SelectImage(NSObject sender, MonoTouch.UIKit.UIEvent @event) {
4: //UIImagePickerController picker = new UIImagePickerController ();
5: //picker.ModalPresentationStyle = UIModalPresentationStyle.FullScreen;
6: //picker.Delegate = new MyImagePickerDelegate (this);
7: //this.PresentModalViewController (picker,true);
8:
9: if (popOver == null || popOver.ContentViewController == null)
10: {
11: imagePicker = new UIImagePickerController();
12: popOver = new UIPopoverController(imagePicker);
13: ImagePickerDelegate imgDel = new ImagePickerDelegate();
14: imagePicker.Delegate = imgDel;
15: imagePicker.SourceType = UIImagePickerControllerSourceType.PhotoLibrary;
16: }
17: if (popOver.PopoverVisible)
18: {
19: popOver.Dismiss(true);
20: imagePicker.Dispose();
21: popOver.Dispose();
22: return;
23: }
24: else
25: {
26: popOver.PresentFromRect(((UIButton)sender).Frame, this.View, UIPopoverArrowDirection.Any, true);
27: }
28:
29: }
public override void ViewDidLoad()
{
imagePicker = new UIImagePickerController();
popOver = new UIPopoverController(imagePicker);
base.ViewDidLoad();
}注意把2个Control设置成静态的,否则会报错[UIPopoverController dealloc] reached while popover is still visible.
也可以不设置成静态变量,但要声明为类成员变量或属性,然后再ViewDidLoad方法初始化,就是避免在要使用的时候已经被GC回收了。
原因还是GC,所以一个很好的做法是UIPopoverControllers应始终保持在一个实例变量。或建立一个强类型属性。
1: public class ImagePickerDelegate : UIImagePickerControllerDelegate
2: {
3: public ImagePickerDelegate()
4: {}
5:
6: public override void FinishedPickingMedia(UIImagePickerController picker, NSDictionary info)
7: {
8: UIImage image = (UIImage)info.ObjectForKey(new NSString("UIImagePickerControllerOriginalImage"));
9: // do whatever else you'd like to with the image
10: }
11: }
总结:
- UIImagePickerController必须由UIPopoverController来承载。
- 如果presentModalViewController的UIImagePickerController的SourceType为设置到“UIImagePickerControllerSourceTypeCamera”时。设备必须是iPad 2的相机在全屏幕视图。最大化才是是正确的UIModalPresentationStyle.FullScreen,presentModalViewController的sourceType被设置成任何其他的情况都会崩溃。UIImagePickerController Class Reference
- UIPopoverControllers应始终保持在一个实例变量。或建立一个强类型属性保存。
作者:Bruce Lee
出处:http://www.cnblogs.com/BruceLee521
本博原创文章版权归博客园和本人共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出作者名称和原文连接,否则保留追究法律责任的权利。
实际效果