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

  1. 云栖社区>
  2. 博客>
  3. 正文

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

ghost丶桃子 2016-05-18 17:25:55 浏览2339
展开阅读全文

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

 

from:http://blog.csdn.net/fxj281314/article/details/6050588 

欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 330987132 | Go:217696290 | Python:336880185 | 做人要厚道,转载请注明出处!http://www.cnblogs.com/sunshine-anycall/archive/2012/05/30/2526622.html

网友评论

登录后评论
0/500
评论
ghost丶桃子
+ 关注