Call UIImagePickerViewController for iPad and iPhone

Source: Internet
Author: User

IPadAndIPhoneCallUIImagePickerViewControllerThe method is slightly different from the content described in this article.IPadAndIphoneFor details.

We know thatIPhoneTo obtain the Photo Library in the following ways:

 
 
  1. UIImagePickerController *m_imagePicker = [[UIImagePickerController alloc] init];  
  2.     if ([UIImagePickerController isSourceTypeAvailable:  
  3.          UIImagePickerControllerSourceTypePhotoLibrary]) {  
  4.         m_imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  
  5.         m_imagePicker.delegate = self;  
  6.         //        [m_imagePicker.navigationBar.subviews];  
  7.         [m_imagePicker setAllowsEditing:NO];  
  8.         //m_imagePicker.allowsImageEditing = NO;  
  9.         [self presentModalViewController:m_imagePicker animated:YES];  
  10.         [m_imagePicker release];  
  11.     }else {  
  12.         UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:  
  13. @"Error accessing photo library!" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];  
  14.         [alert show];  
  15.         [alert release];  
  16.     } 

This is no problem with iPhone operations. However, when we encounter problems in the iPad environment, the following error will be reported during running:

 
 
  1. Terminating app due to uncaught exception 'NSInvalidArgumentException',   
  2. reason: 'On iPad, UIImagePickerController must be presented via UIPopoverController' 

Therefore, we must use UIPopoverController. The specific implementation is as follows:

 
 
  1. UIImagePickerController *m_imagePicker = [[UIImagePickerController alloc] init];  
  2.     if ([UIImagePickerController isSourceTypeAvailable:  
  3.          UIImagePickerControllerSourceTypePhotoLibrary]) {  
  4.         m_imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  
  5.         m_imagePicker.delegate = self;  
  6.         [m_imagePicker setAllowsEditing:NO];  
  7.         UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:m_imagePicker];  
  8.         self.popoverController = popover;  
  9.         //popoverController.delegate = self;  
  10.            
  11.         [popoverController presentPopoverFromRect:CGRectMake(0, 0, 300, 300) inView:self.
  12. view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];  
  13.            
  14.         //[self presentModalViewController:m_imagePicker animated:YES];  
  15.         [popover release];  
  16.         [m_imagePicker release];  
  17.     }else {  
  18.         UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"Error accessing photo library!" 
  19. delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];  
  20.         [alert show];  
  21.         [alert release];  
  22.     } 

Note that a global UIPopoverController object popoverController is assigned to the partial UIPopoverController object popopovercontroller. Instead, you cannot directly call pover. Because popover objects cannot be released when they are still visible.

Summary:IPadAndIphoneCallUIImagePickerViewControllerThe method is slightly different. I hope this article will help you!

Related Article

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.