Use uiimagepickercontroller to select different codes for monotouch images on iPad and iPhone

Source: Internet
Author: User

Although uiimagepickercontroller can be used on iPad and iPhone, the code is different.

The iPhone code is as follows:

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

This code is normal on the iPhone, but the following exception is reported on the iPad:

Monotouch. Foundation. monotouchexception: Objective-C exception thrown. Name: nsinvalidargumentexception reason: on iPad, uiimagepickercontroller must be presented via uipopovercontroller

Why? It is the same as the exception.Uiimagepickercontroller must be carried by uipopovercontroller.

IPad code:

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

}


Note thatControl (s)Set to static. Otherwise, [uipopovercontroller dealloc] reached while popover is still visible will be reported.

You can also set it to a static variable, but declare it as a class member variable or attribute, and then initialize the viewdidload method to avoid GC recycle when you want to use it.

The cause is GC, so a good practice is that uipopovercontrollers should always stay in an instance variable. Or create a strong property.

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

Summary:

  1. UiimagepickercontrollerRequired by uipopovercontroller.
  2. If presentmodalviewcontrollerUiimagepickercontrollerSourcetypeSet to "uiimagepickercontrollersourcetypecamera". The device must be iPad 2The camera is in the full screen view. The maximization is the correct uimodalpresentationstyle. fullscreen, PresentmodalviewcontrollerSourcetypeIf it is set to any other situation, it will crash.Uiimagepickercontroller class reference

  3. Uipopovercontrollers should always be in an instance variable. Or create a strong property to save.

 

By Bruce Lee
Source: http://www.cnblogs.com/BruceLee521
This blog Original article is copyrighted by the blog and I share it with you. You are welcome to reprint it. However, you must keep this statement without the author's consent and provide the author name and original article connection clearly on the article page, otherwise, you are entitled to pursue legal liability.

 

Actual Effect

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.