iOS 打开设备相册
2013-10-28 16:19
218 查看
在iPhone中获取照片库常用的方法如下:
UIImagePickerController *m_imagePicker = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary]) {
m_imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
m_imagePicker.delegate = self;
// [m_imagePicker.navigationBar.subviews];
[m_imagePicker setAllowsEditing:NO];
//m_imagePicker.allowsImageEditing = NO;
[self presentModalViewController:m_imagePicker animated:YES];
[m_imagePicker release];
}else {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"Error accessing photo library!" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];
[alert show];
[alert release];
}
这对iPhone的操作是没有问题的。但是当我们在iPad环境中却有问题了,当我们运行时会报如下错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'On iPad, UIImagePickerController must be presented via UIPopoverController'
所以我们必须通过UIPopoverController来实现才行。具体实现如下:
UIImagePickerController *m_imagePicker = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary]) {
m_imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
m_imagePicker.delegate = self;
[m_imagePicker setAllowsEditing:NO];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:m_imagePicker];
self.popoverController = popover;
//popoverController.delegate = self;
[popoverController presentPopoverFromRect:CGRectMake(0, 0, 300, 300) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
//[self presentModalViewController:m_imagePicker animated:YES];
[popover release];
[m_imagePicker release];
}else {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"Error accessing photo library!" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];
[alert show];
[alert release];
}
这里需要注意,对局部UIPopoverController对象popover我们赋给了一个全局的UIPopoverController对象popoverController。而不能直接调用popover。因为在popover对象还可见时,是不能够被释放的。
UIImagePickerController *m_imagePicker = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary]) {
m_imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
m_imagePicker.delegate = self;
// [m_imagePicker.navigationBar.subviews];
[m_imagePicker setAllowsEditing:NO];
//m_imagePicker.allowsImageEditing = NO;
[self presentModalViewController:m_imagePicker animated:YES];
[m_imagePicker release];
}else {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"Error accessing photo library!" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];
[alert show];
[alert release];
}
这对iPhone的操作是没有问题的。但是当我们在iPad环境中却有问题了,当我们运行时会报如下错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'On iPad, UIImagePickerController must be presented via UIPopoverController'
所以我们必须通过UIPopoverController来实现才行。具体实现如下:
UIImagePickerController *m_imagePicker = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary]) {
m_imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
m_imagePicker.delegate = self;
[m_imagePicker setAllowsEditing:NO];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:m_imagePicker];
self.popoverController = popover;
//popoverController.delegate = self;
[popoverController presentPopoverFromRect:CGRectMake(0, 0, 300, 300) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
//[self presentModalViewController:m_imagePicker animated:YES];
[popover release];
[m_imagePicker release];
}else {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"Error accessing photo library!" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];
[alert show];
[alert release];
}
这里需要注意,对局部UIPopoverController对象popover我们赋给了一个全局的UIPopoverController对象popoverController。而不能直接调用popover。因为在popover对象还可见时,是不能够被释放的。
相关文章推荐
- iOS开发:iOS10.2手机系统打开相册遇到的各种显示英文文字的问题
- <转载>让iOS的应用程序在点击设备的home键返回桌面后,再次打开应用进入启动画面
- IOS研究院之打开照相机与本地相册选择图片
- 在手机相册(ios设备相册)中创建相册
- iOS 打开系统相册和使用相机
- IOS学习:打开照相机与本地相册选择图片
- 通过手机浏览器,打开ios设备上应用/通过url schema打开本地应用
- 【Ios】 Unity for iOS 打开相册、相机及保存图片到相册
- IOS 横屏 打开相册 问题
- iOS开发小技巧--相机相册的正确打开方式
- iOS 打开相册,保存图片到相册,打开相机
- iOS 打开相册或相机
- iOS打开照相机与本地相册选择图片
- Q4关于Xcode调试设备只显示iOS Device或者打开iOS Simulator提示unable to determined device
- 让iOS的应用程序在点击设备的home键返回桌面后,再次打开应用进入启动画面
- ios判断设备是否打开定位服务
- iOS 打开系统相册和使用相机
- IOS 横屏打开相册
- IOS 打开相机和相册的方法