UIImagePickerController在iPhone和iPad中用法的一点不同

UIImagePickerController在iPhone和iPad中用法的一点不同


 

UIImagePickerController在iPhone和iPad中用法的一点不同

  2033人阅读  评论(6)  收藏  举报

我们知道,在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,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对象还可见时,是不能够被释放的。

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...