Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImagePickerController not presenting in iOS 8

Is anyone else having an issue with UIImagePickerController in iOS 8? The method below works perfectly well in iOS 7 on an iPad, but I get the following error when I run this in XCode 6 (Beta 3 or 4) when I try to present the picker (last line). If it matters, the selection of the sourceType is coming from an alertView that is presented in the same place.

Warning: Attempt to present <UIImagePickerController: 0x7c0ae400>  on <CAGUCreateContactViewController: 0x7bf61a00> which is already presenting (null) 

Method to open imagePicker.

- (void)openPhotoPicker:(UIImagePickerControllerSourceType)sourceType {     if ([UIImagePickerController isSourceTypeAvailable:sourceType]) {         NSArray *availableMediaTypes = [UIImagePickerController availableMediaTypesForSourceType:sourceType];         if ([availableMediaTypes containsObject:(NSString *)kUTTypeImage]) {             UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];             imagePickerController.modalPresentationStyle = UIModalPresentationFullScreen;             imagePickerController.sourceType = sourceType;             imagePickerController.mediaTypes = @[(NSString *)kUTTypeImage];             imagePickerController.delegate = self;              self.imagePickerController = imagePickerController;              if (sourceType == UIImagePickerControllerSourceTypeCamera) {                 [self presentViewController:self.imagePickerController animated:YES completion:nil];             } else {                                     if (self.popoverVC) {                     [self.popoverVC dismissPopoverAnimated:YES];                     self.popoverVC = nil;                 }                  self.popoverVC = [[UIPopoverController alloc] initWithContentViewController:imagePickerController];                 [self.popoverVC presentPopoverFromRect:self.nameAndPicCell.picture.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];             }         }     } } 
like image 390
Dave Avatar asked Jul 24 '14 19:07

Dave


People also ask

What is uiimagepickercontroller in iOS?

For example, Facebook, Twitter and WhatsApp, etc. Apple provides a simple-to-use tool UIImagePickerController to allow developer to implement this feature within app in an easy way. Developer can define the media type (photo or video) and the source (user’s album or camera) of file.

Which photo types can be supported by uiimagepickerviewcontroller?

Not all photo types can be supported by UIImagePickerViewController. For example, panorama, live photos, time-lapse and slow motion UIImagePickerViewController provides an easy to use API for app user to select either photo or video from device. Media files can be selected or captured from Moments ,album or camera.

What is an image picker controller?

A view controller that manages the system interfaces for taking pictures, recording movies, and choosing items from the user's media library. An image picker controller manages user interactions and delivers the results of those interactions to a delegate object.

How to create a fully-customized image picker for browsing photo library?

To create a fully-customized image picker for browsing the photo library, use classes from the Photos framework.


2 Answers

I agree with Ben Lings issue detection. I would suggest a simpler solution in case when using UIActionSheet. I simply moved my code that reacts on Action Sheet selection from:

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex; { // my code } 

into:

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex;  // after animation { // my code } 

This way app is guarantied that code will be executed AFTER UIActionSheet animation finishes.

Since UIAlertView has similar delegate method:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;  // after animation { // my code } 

I suppose that similar solution may apply.

like image 31
vedrano Avatar answered Sep 30 '22 00:09

vedrano


I think this is because in iOS 8, alert views and action sheets are actually presented view controllers (UIAlertController). So, if you're presenting a new view controller in response to an action from the UIAlertView, it's being presented while the UIAlertController is being dismissed. I worked around this by delaying the presentation of the UIImagePickerController until the next iteration of the runloop, by doing this:

[[NSOperationQueue mainQueue] addOperationWithBlock:^{     [self openPhotoPicker:sourceType]; }]; 

However, the proper way to fix this is to use the new UIAlertController API on iOS 8 (i.e. use if ([UIAlertController class]) ... to test for it). This is just a workaround if you can't use the new API yet.

like image 173
Ben Lings Avatar answered Sep 30 '22 01:09

Ben Lings