0

我对此进行了很多研究。触摸按钮时,我必须从设备中选择图像并显示在屏幕上。

但是当按钮被触摸/按下时,它会导致我的应用程序崩溃。它从这行代码崩溃:

[self presentModalViewController:myPhotopicker animated:YES];

我正在使用 Xcode 4.2 开发 iPad 应用程序。我正在使用 iPad 5.0 模拟器进行测试。我的系统在 Mac OS X 版本 10.6.8 上运行。

按下按钮时调用以下函数:

-(IBAction)getPhoto:(id)sender
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        if (myPhotopicker==nil) { myPhotopicker = [[UIImagePickerController alloc] init];
            myPhotopicker.delegate = self; }// create once!
        myPhotopicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

        [self presentModalViewController:myPhotopicker animated:YES];

    } else {
        NSString *str = @"Photo Album is not available!";     

    }   

}
4

1 回答 1

0

我尝试了您的代码,并且可以在模拟器中重现崩溃。但它在我的带有 iOS 4.2 的 iPhone 4 上运行良好。

也就是说,我在模拟器中制作了我的画廊,其中包含一些照片。(启动模拟器中的 Safari,打开一些页面,通过长按它们并从菜单中选择保存来保存一些图片。)

现在,模拟器写道

2012-05-08 15:53:55.605 test[5870:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'On iPad, UIImagePickerController must be presented via UIPopoverController'

到控制台。

好的,阅读,完成:

-(IBAction)getPhoto:(UIButton *)sender
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) 
    {
        if (myPhotopicker==nil) {
            myPhotopicker = [[UIImagePickerController alloc] init];
            myPhotopicker.delegate = self;
        }// create once!
        myPhotopicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            // iPad Code:
            UIPopoverController *popover =
                [[UIPopoverController alloc] initWithContentViewController:myPhotopicker];
            [popover presentPopoverFromRect:sender.bounds
                                     inView:self.view
                   permittedArrowDirections:UIPopoverArrowDirectionAny
                                   animated:YES];
        } else {
            // iPhone Code:
            [self presentModalViewController:myPhotopicker animated:YES];
        }
    } else {
        NSLog(@"Photo Album is not available!");
    }     
}

现在它看起来像这样:
在此处输入图像描述

于 2012-05-08T14:04:40.560 回答