Mason
Mason

Reputation: 7103

UIImagePickerViewerController crashes when attempting to go to saved photos (on iPhone)

I have a UIImagePickerViewerController. It works perfectly when I select UIImagePickerControllerSourceTypeCamera. However, when I go try to select UIImagePickerControllerSourceTypeSavedPhotosAlbum, it crashes with this error:

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

Here is the code I have:

        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];

Any help is greatly appreciated! Thanks in advance!

Upvotes: 4

Views: 5309

Answers (3)

Daniel
Daniel

Reputation: 22405

Your mistake is in this line

[picker setShowsCameraControls:YES];

which is where the exception is thrown, the problem is you cannot set showCameraControls to yes when you are using the album. Just comment that line out and you should be fine.

Upvotes: 8

Madhu
Madhu

Reputation: 2384

I think the issue is that you're setting the sourceType to UIImagePickerControllerSourceTypeSavedPhotosAlbum regardless of which sourceType is supported.

i.e. The following line should be conditional;

[picker setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];

Instead, put in a condition or two, and if the UIImagePickerControllerSourceTypeSavedPhotosAlbum exists, then only set the sourceType to this one. Else, if the UIImagePickerControllerSourceTypeCamera exists, set the sourceType to that one.

Also, I guess [picker setShowsCameraControls:YES]; should be done for the UIImagePickerControllerSourceTypeCamera sourceType only.

The error you're getting is suggesting that UIImagePickerControllerSourceTypeSavedPhotosAlbum is not supported, so try working it out in this manner.

Upvotes: 3

Hardik Soni
Hardik Soni

Reputation: 97

What you need to do is... Change your if condition in else if then or.. when

if([isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{

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

Upvotes: 2

Related Questions