UIImagePickerController在iPhone和iPad上的区别

在iPhone中获取照片库的常用方法如下: 

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

 

这在iPhone下操作是没有问题的,但在iPad下就会有问题了,运行时会报出下面的错误:

 

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

 

所以,我们必须通过UIPopoverController来实现才行。具体的实现如下:

 

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

转自:http://2015.iteye.com/blog/1340797

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值