iOS   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了UIImagePickerController在iPhone和iPad中用法的一点不同大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

源自:http://huchenqiang90.blog.163.com/blog/static/1125008002011018416051/


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

大佬总结

以上是大佬教程为你收集整理的UIImagePickerController在iPhone和iPad中用法的一点不同全部内容,希望文章能够帮你解决UIImagePickerController在iPhone和iPad中用法的一点不同所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: