Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'
like image 918
Arun Avatar asked Feb 07 '13 06:02

Arun


2 Answers

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.

like image 134
iDev Avatar answered Oct 22 '22 02:10

iDev


@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;
like image 42
Maddy Avatar answered Oct 22 '22 00:10

Maddy