尝试去保存照片时,UIImagePickerViewController崩溃(在iPhone上)

时间:2011-09-14 05:47:43

标签: iphone objective-c ios uiimagepickercontroller

我有一个UIImagePickerViewerController。当我选择UIImagePickerControllerSourceTypeCamera时它完美无缺。但是,当我尝试选择UIImagePickerControllerSourceTypeSavedPhotosAlbum时,它会因此错误而崩溃:

2011-09-14 01:41:21.779 NG911[378:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Source type must be UIImagePickerControllerSourceTypeCamera'

这是我的代码:

        if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum] || ![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        UIAlertView *noCameraAlert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                message:@"This device does not support a photo library"
                                                               delegate:nil
                                                      cancelButtonTitle:@"OK"
                                                      otherButtonTitles:nil, nil];
        [noCameraAlert setTag:2];
        [noCameraAlert show];
        [noCameraAlert release];
        return;
    }

    [picker setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
    [picker setShowsCameraControls:YES];
    [picker setAllowsEditing:NO];
    [self presentModalViewController:picker animated:YES];

非常感谢任何帮助!提前谢谢!

3 个答案:

答案 0 :(得分:7)

你的错误就在这一行

[picker setShowsCameraControls:YES];

这是引发异常的地方,问题是当您使用相册时,无法将showCameraControls设置为yes。只要评论一下就行,你应该没问题。

答案 1 :(得分:3)

我认为问题在于您将sourceType设置为UIImagePickerControllerSourceTypeSavedPhotosAlbum,无论支持哪种sourceType。

即。以下行应该是有条件的;

[picker setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];

相反,放入一个或两个条件,如果UIImagePickerControllerSourceTypeSavedPhotosAlbum存在,则只将sourceType设置为此值。否则,如果UIImagePickerControllerSourceTypeCamera存在,请将sourceType设置为该类型。

另外,我猜[picker setShowsCameraControls:YES];应仅针对UIImagePickerControllerSourceTypeCamera sourceType进行。

您收到的错误表明不支持UIImagePickerControllerSourceTypeSavedPhotosAlbum,因此请尝试以这种方式进行处理。

答案 2 :(得分:2)

你需要做的是......如果那样改变你的if条件,或者......当

if([isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{

[picker setShowsCameraControls:NO];
}
else
{
[picker setShowsCameraControls:YES];
}