iPod Touch (5th gen) 'Source type 1 not available' crash when using UIImagePickerControllerSourceTypeCamera

249 views Asked by At

The iPod Touch (5th gen) has both a front and rear camera so why does my app crash when i try to present a UIImagePickerController with sourceType: UIImagePickerControllerSourceTypeCamera

- (void)openImagePickerType:(NSString *)type
{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    if([type isEqualToString:kImagePickerCameraString])
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    if([type isEqualToString:kImagePickerLibraryString])
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.allowsEditing = NO;

    if(IS_IPAD && imagePicker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary){
        DLog(@"show popopver image picker");


        UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
        [popover presentPopoverFromRect:self.cameraButton.bounds inView:self.cameraButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
        self.popover = popover;

    }else{
        [self presentViewController:imagePicker animated:YES completion:nil];
    }


}
1

There are 1 answers

2
Fantini On BEST ANSWER

First, You should always validate if the resource is available or not, try using:

if(IS_IPAD && imagePicker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary){

  if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
    // present the popover
  }

  else {
    // HEY, YOU CAN'T USE THIS
  }

}

From the official docs:

Because a media source may not be present or may be unavailable, devices may not always support all source types. For example, if you attempt to pick an image from the user’s library and the library is empty, this method returns false. Similarly, if the camera is already in use, this method returns false.

Before attempting to use an UIImagePickerController object to pick an image, you must call this method to ensure that the desired source type is available.

Second, I don't see clearly when this part should be executed:

else {
  [self presentViewController:imagePicker animated:YES completion:nil];
}

You should specify something like this:

if(IS_IPAD && imagePicker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary){

  if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
    // present the popover
  }
  else {
    // HEY, YOU CAN'T USE THIS
  }

else if (imagePicker.sourceType == UIImagePickerControllerSourceTypeCamera) {

  if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
    // present the popover
  }
  else {
    // HEY, YOU CAN'T USE THIS
  }

  ...

}
  1. Why use an NSString type for type var when you can always use directly UIImagePickerControllerSourceType as you var's type