获取手机照片UIImagePickerViewController

iPadiPhone调用UIImagePickerViewController方法略有不同是本文要介绍的内容,文中很详细的讲述了iPadiphone各自的调用方法,来看详细内容。

我们知道,在iPhone中获取照片库常用的方法如下:

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

这对iPhone的操作是没有问题的。但是当我们在iPad环境中却有问题了,当我们运行时会报如下错误:

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

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

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

这里需要注意,对局部UIPopoverController对象popover我们赋给了一个全局的UIPopoverController对象popoverController。而不能直接调用popover。因为在popover对象还可见时,是不能够被释放的。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值