Search code examples
iphoneiosobjective-cipadios6

Exception in iPad, UIImagePickerController must be presented via UIPopoverController


I have created an application for capture image from camera. This is my code

 -(IBAction) showCameraUI {
    BOOL hasCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
    UIImagePickerController* picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = hasCamera ? UIImagePickerControllerSourceTypeCamera :    UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentModalViewController:picker animated:YES];
}

And implemented this delegate method for get the captured image

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissModalViewControllerAnimated:YES];
    UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
    UIImage *yourImageView = image;
}

Implemented this method if user cancel the controller

- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker
{
    [picker dismissModalViewControllerAnimated:YES];
}

But it shows this exception. Does anyone have any idea why it is showing such exception after executing last line of function showCameraUI.

UIStatusBarStyleBlackTranslucent is not available on this device. 2013-02-07 
10:06:06.976 CaptureImage[460:c07] *** Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: 'On iPad, UIImagePickerController must be 
presented via UIPopoverController'

Solution

  • Regarding the exception, the error message is very clear. "On iPad, UIImagePickerController must be presented via UIPopoverController" For iPad, you should present it in a UIPopoverController instead of using [self presentModalViewController:picker animated:YES];. This should fix the issue.

    For eg:-

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:picker];
        [popover presentPopoverFromRect:self.view.bounds inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
        self.popover = popover;
    } else {
        [self presentModalViewController:picker animated:YES];
    }
    

    Edit: As mentioned by @rmaddy, camera can be presented modally. The above is applicable when sourceType is UIImagePickerControllerSourceTypePhotoLibrary.