首页 技术 正文
技术 2022年11月21日
0 收藏 930 点赞 4,270 浏览 3209 个字
本文转载至:http://blog.csdn.net/k12104/article/details/8537695On iPad, UIImagePickerController must be presented via UIPopoverController  可以用以下方法来判断设备的类型选择不同的Controller
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone){// We are using an iPhoneUIActionSheet*alertSheet =[[UIActionSheet alloc] initWithTitle:@"Where do you want to get your daily image?" delegate:(self) cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Camera",@"Library", nil];[alertSheet setTag:0];[alertSheet setDelegate:self];[alertSheet showFromTabBar:[[self tabBarController] tabBar]];[alertSheet release];}else{// We are using an iPadUIImagePickerController*imagePickerController =[[UIImagePickerController alloc] init];
imagePickerController.delegate = self;UIPopoverController*popoverController=[[UIPopoverController alloc] initWithContentViewController:imagePickerController];
popoverController.delegate=self;[popoverController presentPopoverFromRect:((UIButton*)sender).bounds inView:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];}

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

我们知道,在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. [popoverController presentPopoverFromRect:CGRectMake(0, 0, 300, 300) inView:self.
  11. view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
  12. //[self presentModalViewController:m_imagePicker animated:YES];
  13. [popover release];
  14. [m_imagePicker release];
  15. }else {
  16. UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@”Error accessing photo library!”
  17. delegate:nil cancelButtonTitle:@”Close” otherButtonTitles:nil];
  18. [alert show];
  19. [alert release];
  20. }

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

小结:iPad与iphone调用UIImagePickerViewController方法略有不同的内容介绍完了,希望本文对你有所帮助!

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,992
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,506
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,349
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,134
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,767
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,844