UIImagePickerController在iPad上崩溃

10
-(IBAction)selectPressed:(id)sender
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentModalViewController:picker animated:YES];
    [picker release];
}

我正在iPad和iPhone模拟器上测试这段代码。在iPhone模拟器上(以及真实的iPhone设备上)它可以正常运行 - 画廊会出现。但是在iPad模拟器上(我没有这个设备),它会崩溃。有任何想法原因是什么?


请查看这个答案:https://dev59.com/WWox5IYBdhLWcg3wtmnh#9019460 - neoneye
4个回答

14
请在设备日志中查看异常消息:

在iPad上,UIImagePickerController必须通过UIPopoverController来呈现


3
根据文档http://developer.apple.com/library/ios/#documentation/uikit/reference/UIImagePickerController_Class/UIImagePickerController/UIImagePickerController.html, "在iPad上,您可以选择使用弹出窗口来呈现用户界面"(遵循使用模态视图控制器的标准方式)。因此,根据文档,错误消息只是UIKit中的一个错误。 - user8472
1
iOS7 现在允许UIImagePickerController全屏模态呈现,我还没有尝试FormSheet等,但应该是一样的! - theiOSDude

7

Max表示:

在iPad上,必须通过UIPopoverController来呈现UIImagePickerController

现在似乎可以使用presentModalViewController来呈现UIImagePickerController,只要其sourceType设置为UIImagePickerControllerSourceTypeCamera。这应该是为了支持iPad 2的相机在全屏视图下拍照。Max正确指出当sourceType设置为其他任何值时,在iPad上使用presentModalViewController会导致崩溃。


从什么时候开始?哪个iOS版本? - openfrog
我发现我的iPad2相反。相机奔溃了。照片库运行良好。 - AlvinfromDiaspar

2
在iPad上显示模态视图控制器时,该视图控制器还需要设置其modalPresentationStyle属性才能显示传入的视图。
以下是文档提供的可用选项:
typedef enum {
   UIModalPresentationFullScreen = 0,
   UIModalPresentationPageSheet,
   UIModalPresentationFormSheet,
   UIModalPresentationCurrentContext,
} UIModalPresentationStyle;

我尝试设置 picker.modalPresentationStyle = UIModalPresentationFullScreen; 或者 self.modalPresentationStyle = UIModalPresentationFullScreen; 但是没有帮助。 - spe
1
请查看@Max的回答。我不知道UIImagePickerController在iPad上有这个特定的要求。 - Mark Adams

2

尝试使用此代码来访问iPad相册库:

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
[popover presentPopoverFromRect:CGRectMake(0.0, 0.0, 400.0, 400.0) 
                         inView:self.view
       permittedArrowDirections:UIPopoverArrowDirectionAny 
                       animated:YES];

或者http://www.techotopia.com/index.php/An_Example_iOS_4_iPad_Camera_and_UIImagePickerController_Application_(Xcode_4) 参考资料


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接