Exception in iPad, UIImagePickerController must be

2019-02-16 13:00发布

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'

3条回答
SAY GOODBYE
2楼-- · 2019-02-16 13:06

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.

查看更多
迷人小祖宗
3楼-- · 2019-02-16 13:09

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
查看更多
The star\"
4楼-- · 2019-02-16 13:17

@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;
查看更多
登录 后发表回答