本文转载至:http://blog.csdn.net/k12104/article/details/8537695

On 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方法略有不同的内容介绍完了,希望本文对你有所帮助!

On iPad, UIImagePickerController must be presented via UIPopoverController的更多相关文章

  1. iOS:iPad和iPhone开发的异同(UIPopoverController、UISplitViewController)

    iPad和iPhone开发的异同 1.iPhone和iPad: niPhone是手机,iPad.iPad Mini是平板电脑 iPhone和iPad开发的区别 屏幕的尺寸 \分辨率 UI元素的排布 \ ...

  2. IOS成长之路-调用照相机和相册功能

    打开相机: //先设定sourceType为相机,然后判断相机是否可用(ipod)没相机,不可用将sourceType设定为相片库 UIImagePickerControllerSourceType ...

  3. iPad 控件 UIPopoverPresentationController 使用 iPhone可用

    UIPopoverController 在iOS9之后被废弃了,,, iOS8 新控件UIPopoverPresentationController可运用在iphone和iPad上,使用基本同 UIP ...

  4. BlocksKit的使用

    一.引言 众所周知Block已被广泛用于iOS编程.它们通常被用作可并发执行的逻辑单元的封装,或者作为事件触发的回调.Block比传统回调函数有2点优势: 允许在调用点上下文书写执行逻辑,不用分离函数 ...

  5. IOS 在Ipad 横屏 上使用UIImagePickerController

    转载前请注明来源:http://www.cnblogs.com/niit-soft-518/p/4381328.html 最近在写一个ipad的项目,该项目必须是横屏.进入正题,有一项功能是要调用系统 ...

  6. iPad 控件UIPopoverController使用

    UIPopoverController 是iPad特有控件,(iOS7-9),在iOS9之后别废弃 使用步骤 设置内容控制器 UIPopoverController直接继承NSObject,不具备可视 ...

  7. iOS iPad开发之UIPopoverController的使用

    1. 什么是UIPopoverController? 是iPad开发中常见的一种控制器(在iphone上不允许使用) 跟其他控制器不一样的是,它直接继承自NSObject,并非继承自UIViewCon ...

  8. iOS8 iPad Warning: Attempt to present <UIImagePickerController:xxxx > on xxxx which is already presenting (null)

    解决方法: /* I think this is because in iOS 8, alert views and action sheets are actually presented view ...

  9. iPad开发中UIPopoverController的使用

    什么是UIPopoverController 是iPad开发中常见的一种控制器 跟其他控制器不一样的是,它直接继承自NSObject,并非继承自UIViewController 它只占用部分屏幕空间来 ...

随机推荐

  1. oracle中普通视图和实体试图的区别

    Oracle普通视图和实体化视图比较 来源:茂盛博客|2013-07-30 Oracle普通视图和实体化视图比较 相对于普通的视图来说,实体化视图的不同之处在于实体化视图管理存储数据,占据数据库的物理 ...

  2. [ubuntu]E: The package firmware-upgrade needs to be reinstalled, but I can't find an archive for it.

    解决办法把firmware-upgrade卸载 sudo dpkg --remove --force-all firmware-upgrade 然后 sudo apt-get update 即可

  3. c++的内存分配

    C++堆和栈的分配 腾讯.金山笔试常考 栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等,其操作方式类似于数据结构中的栈. 堆区(heap) — 一般由程序员分配释放, ...

  4. [LintCode]判断一个字符串是否包含另一个字符串的所有字符

    问题描述: 比较两个字符串A和B,确定A中是否包含B中所有的字符.字符串A和B中的字符都是 大写字母. 样例 给出 A = "ABCD" B = "ACD",返 ...

  5. Linux启动与禁止SSH用户及IP的登录

    以下就针对SSH方面讨论一下.假设有人特别关注Linux环境的安全性,第一就从login方面来进行讨论 1:Linux启动或禁止SSH root用户的登录 2:Linux限制SSH用户 事实上这些东西 ...

  6. lvm 新建一个vg 逻辑卷 配置启动

    fdisk /dev/sdb 格式 t  8e w vgcreate datavg /dev/sdb1lvcreate -L 999G -n lvdata datavgmkfs.xfs /dev/da ...

  7. Entity Framework应用:使用Code First模式管理数据库创建和填充种子数据

    一.管理数据库连接 1.使用配置文件管理连接之约定 在数据库上下文类中,如果我们只继承了无参数的DbContext,并且在配置文件中创建了和数据库上下文类同名的连接字符串,那么EF会使用该连接字符串自 ...

  8. 【F12】网络面板

    使用网络面板了解请求和下载的资源文件并优化网页加载性能 (1)网络面板基础 测量资源加载时间 使用 Network 面板测量您的网站网络性能. Network 面板记录页面上每个网络操作的相关信息,包 ...

  9. C++ Primer学习笔记(二)

    题外话:一工作起来就没有大段的时间学习了,如何充分利用碎片时间是个好问题. 接  C++ Primer学习笔记(一)   27.与 vector 类型相比,数组的显著缺陷在于:数组的长度是固定的,无法 ...

  10. 如果不得已需要全局变量,则使全局变量加前缀 g_(表示 global)

    如果不得已需要全局变量,则使全局变量加前缀 g_(表示 global). 例如: int g_howManyPeople; // 全局变量 int g_howMuchMoney; // 全局变量 #i ...