UIImagePickerViewerController crashes when trying to navigate to saved photos (on iPhone)

I have a UIImagePickerViewerController. It works great when I select UIImagePickerControllerSourceTypeCamera . However, when I 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 much appreciated! Thanks in advance!

+4
source share
3 answers

Your mistake on this line

 [picker setShowsCameraControls:YES]; 

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

+7
source

I think the problem is that you are setting sourceType to UIImagePickerControllerSourceTypeSavedPhotosAlbum, no matter what type of sourceType is supported.

i.e. The next line should be conditional;

 [picker setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum]; 

Instead, put a condition or two, and if there is a UIImagePickerControllerSourceTypeSavedPhotosAlbum , just set it to sourceType. Otherwise, if a UIImagePickerControllerSourceTypeCamera exists, set it to sourceType.

Also, I think [picker setShowsCameraControls: YES]; should be performed only for the source type UIImagePickerControllerSourceTypeCamera.

The error you get means that UIImagePickerControllerSourceTypeSavedPhotosAlbum not supported, so try to do it this way.

+3
source

What you need to do ... Change the if condition otherwise, if then or .. when

 if([isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) { [picker setShowsCameraControls:NO]; } else { [picker setShowsCameraControls:YES]; } 
+2
source

All Articles