On iPad, UIImagePickerController must be presented via UIPopoverController
2013-01-24 13:31
417 查看
[code]On iPad, UIImagePickerController must be presented via UIPopoverController 可以用以下方法来判断设备的类型选择不同的Controller
[code]if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { // We are using an iPhone UIActionSheet *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 iPad UIImagePickerController *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]; }
[code]
[code]iPad与iPhone调用UIImagePickerViewController方法略有不同是本文要介绍的内容,文中很详细的讲述了iPad与iphone各自的调用方法,来看详细内容。我们知道,在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];
}
[/code]这对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对象还可见时,是不能够被释放的。小结:iPad与iphone调用UIImagePickerViewController方法略有不同的内容介绍完了,希望本文对你有所帮助!
相关文章推荐
- On iPad, UIImagePickerController must be presented via UIPopoverController
- On iPad, UIImagePickerController must be presented via UIPopoverController
- On iPad, UIImagePickerController must be presented via UIPopoverController
- On iPad, UIImagePickerController must be presented via UIPopoverController
- UIImagePickerController在iPhone和iPad中用法的一点不同[转]
- UIImagePickerController在UIPopoverController中 旋屏问题
- IOS6 UIImagePickerController在IPAD横屏闪退解决办法
- IOS 在Ipad 横屏 上使用UIImagePickerController
- UIImagePickerController在iPhone和iPad中用法的一点不同
- UIImagePickerController在iPhone和iPad中用法的一点不同
- UIImagePickerController在iPhone和iPad上的区别
- UIImagePickerController在iPhone和iPad中用法的一点不同
- ios8.0 ipad UIImagePickerController
- setObjectForKey: object cannot be nil (key: UIImagePickerControllerOriginalImage)
- UIImagePickerController在iPhone和iPad中用法的一点不同
- Monotouch在IPAD与IPhone使用UIImagePickerController的图片选择不同的代码
- UIImagePickerController在iPhone和iPad上的区别
- UIImagePickerController在iPhone和iPad中用法的一点不同
- 用UIImagePickerViewController自定义相机界面
- AS使用lombok注解报错:Annotation processors must be explicitly declared now. The following dependencies on the compile classpath are found to contain annotation processor.