Exception in iPad, UIImagePickerController must be

2019-02-16 13:21发布

问题:

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'

回答1:

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.



回答2:

@Arun i am also face the same problem add global property in header file.

I hope the below code is useful for you

UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
   [imgPicker setDelegate:self];
   [imgPicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
   [imgPicker setAllowsEditing:YES];
   [imgPicker setModalPresentationStyle:UIModalPresentationCurrentContext];

   UIPopoverController *popOver = [[UIPopoverController alloc] initWithContentViewController:imgPicker];
   popOver.delegate = self;
   self.popoverImageViewController = popOver;
   [self.popoverImageViewController presentPopoverFromRect:CGRectMake(0, 0, 160, 40) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

In that header file create global property like this

@property (strong) UIPopoverController *popoverImageViewController;


回答3:

For Swift with iOS11:

            videoEditor.modalPresentationStyle = .popover
            self.present(picker, animated: true)

            let popover = picker.popoverPresentationController
            popover?.sourceView = self.view

            // then config popover frame and arrow style here