230

In iOS 8 I am having problem capturing images from camera till now I am using this code for

UIImagePickerController *controller=[[UIImagePickerController alloc] init];
controller.videoQuality=UIImagePickerControllerQualityTypeMedium;
controller.delegate=(id)self;
controller.sourceType=UIImagePickerControllerSourceTypeCamera;
[self presentViewController:controller animated:YES completion:nil];

But in iOS 8 I am getting this:

Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.

I have tried with the solution provided by This Post with

@property (strong,nonatomic)UIImagePickerController *controller;

_controller=[[UIImagePickerController alloc] init];
_controller.videoQuality=UIImagePickerControllerQualityTypeMedium;
_controller.delegate=(id)self;
_controller.sourceType=UIImagePickerControllerSourceTypeCamera;
_[self presentViewController:controller animated:YES completion:nil];

and this

...
controller.modalPresentationStyle=UIModalPresentationFullScreen;
or
controller.modalPresentationStyle=UIModalPresentationCurrentContext;
...

and this

double delayInSeconds = 0.1;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    [self presentViewController:controller animated:YES completion:nil];
});

and this

[self presentViewController:controller animated:YES completion:NULL];

and this

[self presentViewController:controller animated:YES completion:^{

}];

any idea?

Community
  • 1
  • 1
souvickcse
  • 7,742
  • 5
  • 37
  • 64

20 Answers20

130

I'm pretty sure this is just a bug in iOS 8.0. It's reproducible with the simplest of POC apps that does nothing more than attempt to present a UIImagePickerController like you're doing above. Furthermore, there's no alternative pattern to displaying the image picker/camera, to my knowledge. You can even download Apple's Using UIImagePickerController sample app, run it, and it will generate the same error out of the box.

That said, the functionality still works for me. Other than the warning/error, do you have issues with the functioning of your app?

KevinH
  • 2,034
  • 1
  • 12
  • 12
  • 2
    Yeah probably it is a bug, code is working fine for me as well. – souvickcse Sep 24 '14 at 05:52
  • 1
    I have a problem with it. When I present the UIImagePickerController from a UIViewController everything works well. But, if that UIViewController is not it's UINavigationController's rootViewController, the popViewController animation does not work properly. Instead the transition happens without an animation. – dfmuir Sep 24 '14 at 19:56
  • 4
    @souvickcse I'm encountering this error as well. Whenever it occurs, the image preview that pops up once a new picture has been taken is completely black. However, the "Re-Take" and "Use Photo" buttons at the bottom of the screen still appear and function properly. Are you witnessing this same behavior? – Barry Beerman Sep 25 '14 at 15:26
  • 1
    @BarryBeerman actually the code is working fine for me, can you share your code – souvickcse Sep 25 '14 at 18:39
  • 3
    @souvickcse I'm using the latest phonegap camera plugin baseline located here: https://github.com/apache/cordova-plugin-camera. Everything works fine with iOS 7.1 running on an iPhone 5 but I can't find a way to get it working properly using iOS 8 on an iPhone 6. – Barry Beerman Sep 25 '14 at 19:16
  • 1
    The same for me guys. iOS 8.1 Beta 2 – Anton Oct 11 '14 at 18:31
  • 2
    The same for me with iOS8.1 final – fvisticot Oct 27 '14 at 21:29
  • 1
    Is there any fix to this? I am stuck with this. It's happening in iOS 8.1 even. – Rashmi Ranjan mallick Nov 05 '14 at 13:13
  • 1
    I was just resizing and moving elements when device orientation changed and this message popped in console. Not even iOS8 but 7.1 simulator. – Pahnev Nov 06 '14 at 00:56
  • 1
    I am getting the black color screen after presenting the camera imagePickerController second time. can suggest anyone? – Anand Gautam Jan 14 '15 at 13:17
  • 1
    @BarryBeerman I had the exact same issue as you. See my answer below. – Dan Jun 09 '15 at 16:39
  • I not even doing anything related to using the camera and I get this spurious warning -- I'm adding a window to the main view just as the app is resigning the active state (for hiding sensitive info). – wcochran Jul 16 '15 at 20:10
  • 4
    Still present in IOS 9 – the Reverend Feb 26 '16 at 17:15
  • my app won't reject right when I am getting this error. – Himanth Jul 28 '16 at 07:16
  • 2
    Still there in iOS 10.3. – Şafak Gezer Apr 25 '17 at 09:48
  • 1
    Still there in iOS 12.2 Lol. – Marlhex May 22 '19 at 23:16
  • This error is generated in the console when I spawn an actionSheet in a certain way. – iRon111 Nov 21 '19 at 23:20
  • Stil here in iOS 14 – Jalil Sep 30 '20 at 22:54
45

I was struggling with this issue for several hours, i have read every relevant topic and found out that the error was caused because under the privacy settings of my device, the camera access to my app was blocked!!! I have never denied access to camera and i don't know how it was blocked but that was the problem!

Pantelis Proios
  • 1,359
  • 1
  • 20
  • 32
  • 4
    i'm not sure if it actually worked for you, but it didn't work for me. I just hope that @KevinH is correct. – Deepak Thakur Nov 24 '14 at 10:44
  • 4
    Nope, the camera access is checked ON for that app in my case. But I do believe this could be caused albeit indirectly by settings: changes to the settings do not sometimes take effect immediately, someone in cupertino must have optimized a call to synchronize somewhere – Anton Tropashko Jan 16 '15 at 12:27
  • This suggestion resolved the issue for me. I had earlier touched "Don't Allow" when the camera selector came up in my app. I thought it would ask me each time I accessed the camera, but it didn't. I had to go to Settings > Privacy > Camera and turn on the slider for my app. Then it worked properly. Seems sorta silly on Apple's part not to ask each time ... – John Contarino Dec 09 '15 at 17:45
  • exactly, sometimes we cant think issue raised for this type of mistakes. thx buddy very helpful – Mr.Javed Multani Dec 01 '16 at 04:52
  • The best option is before using the camera to detect if the capability is active, and if not, alert the user that taking pictures is not available until the camera is activated on the settings. On iOS 10, is possible to send the user to the specific app settings (was possible before iOS 8, but not possible in iOS 8 and 9, if i recall correctly). – kindaian Jul 05 '17 at 10:54
33

I don't have enough reputation points to comment on @greg's answer above, so will add my observations here. I have a Swift project for both iPad and iPhone. I have a method inside my main view controller (relevant bit below). When I test this on a phone, everything works properly and no warnings are generated. When I run it on an iPad, everything works properly but I see the warning about snapshotting the view. The interesting bit, however, is that when I run on an iPad without using the popover controller, everything works properly with no warning. Unfortunately, Apple mandates that the image picker must be used within a popover on iPad, if the camera is not being used.

    dispatch_async(dispatch_get_main_queue(), {
        let imagePicker: UIImagePickerController = UIImagePickerController();
        imagePicker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum;
        imagePicker.mediaTypes = [kUTTypeImage];
        imagePicker.allowsEditing = false;
        imagePicker.delegate = self;

        if(UIDevice.currentDevice().userInterfaceIdiom == .Pad){ // on a tablet, the image picker is supposed to be in a popover
            let popRect: CGRect = buttonRect;
            let popover: UIPopoverController = UIPopoverController(contentViewController: imagePicker);
            popover.presentPopoverFromRect(popRect, inView: self.view, permittedArrowDirections: UIPopoverArrowDirection.Up, animated: true);
        }else{
            self.presentViewController(imagePicker, animated: true, completion: nil);
        }
    });
dlw
  • 511
  • 5
  • 10
  • Looks like this is a correct suggestion because I'm having that error message even with iOS 9.0.2. I have iPhone only app and when I run it on my iPad Mini I see that message when presenting image picker. For some reason iOS thinks that I run iPad app. – John Tracid Oct 11 '15 at 17:10
16

I ran into this after calling UIImagePickerController presentViewController: from the callback to a UIAlertView delegate. I solved the issue by pushing the presentViewController: call off the current execution trace using dispatch_async.

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    dispatch_async(dispatch_get_main_queue(), ^{
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;

        if (buttonIndex == 1)
            imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        else
            imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;

        [self presentViewController: imagePickerController
                           animated: YES
                         completion: nil];
    });
}
greg
  • 1,926
  • 17
  • 26
  • This is what solved it for me. I was attempting to present the UIImagePickerController from inside a UIAlertAction's action block. – josefdlange Nov 05 '15 at 22:15
  • the alertView has been deprecated, would this work if I use the async inside an UIAlertViewController action instead ? – DrPatience Nov 26 '15 at 11:15
  • @DrPatience Sure. dispatch_async works any where you want. I don't know that it's necessary (would depend your code). Read up on GCD - it has a great many uses. – greg Nov 26 '15 at 16:19
12

I had this issue when animating some views and the app would go into background mode and come back. I handled it by setting a flag isActive. I set it to NO in

- (void)applicationWillResignActive:(UIApplication *)application

and YES in

- (void)applicationDidBecomeActive:(UIApplication *)application

and animate or not animate my views accordingly. Took care of the issue.

Pang
  • 9,564
  • 146
  • 81
  • 122
byedissident
  • 181
  • 2
  • 4
11

I had this with an UIAlertControllerStyleActionSheet giving the user the option to take a photo with the camera or use one from library.

I tried a symbolic breakpoint on the error message enter image description here

That showed me the error is produced by the intern use of a UICollectionView during presentation

[self presentViewController:alert animated:YES completion:nil];

enter image description here

I fixed this by explixitly setting the frame before presenting

[alert setPreferredContentSize: alert.view.frame.size];

Here is the complete methode that is working without the error

-(void)showImageSourceAlertFromSender:(id)sender{
UIButton *senderButton = (UIButton*)sender;
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"Camera" style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction *action) {
                                                         [self takePhoto];
                                                     }];
UIAlertAction *libraryAction = [UIAlertAction actionWithTitle:@"Library" style:UIAlertActionStyleDefault
                                                      handler:^(UIAlertAction *action) {
                                                          [self selectPhotoFromLibraryFromSender:sender];
                                                      }];
[alert addAction:cameraAction];
[alert addAction:libraryAction];
alert.popoverPresentationController.delegate = self;
alert.popoverPresentationController.sourceRect = senderButton.frame;
alert.popoverPresentationController.sourceView = self.view;

[alert setPreferredContentSize: alert.view.frame.size];

[self presentViewController:alert animated:YES completion:^(){
}];}
Yedy
  • 2,107
  • 1
  • 25
  • 30
10

You can silence the "Snapshotting a view" warning by referencing the view property before presenting the view controller. Doing so causes the view to load and allows iOS render it before taking the snapshot.

UIAlertController *controller = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
controller.modalPresentationStyle = UIModalPresentationPopover;
controller.popoverPresentationController.barButtonItem = (UIBarButtonItem *)sender;

... setup the UIAlertController ... 

[controller view]; // <--- Add to silence the warning.

[self presentViewController:controller animated:YES completion:nil];
Steve Shepard
  • 221
  • 2
  • 5
  • That is an almost incredibly useful tip. Thank you!!!! (In my case, I still get the warning, but the app no longer crashes.) – Fattie May 17 '16 at 20:49
  • However, when I add a `cameraOverlayView`, unfortunately I still get this problem even when I apply this tip. – Fattie May 17 '16 at 21:02
8

For anyone that is seeing an issue with a black preview after image capture, hiding the status bar after the UIPickerController is shown seems to fix the issue.

UIImagePickerControllerSourceType source = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] ? UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypeSavedPhotosAlbum;
UIImagePickerController *cameraController = [[UIImagePickerController alloc] init];
        cameraController.delegate = self;
        cameraController.sourceType = source;
        cameraController.allowsEditing = YES;
        [self presentViewController:cameraController animated:YES completion:^{
            //iOS 8 bug.  the status bar will sometimes not be hidden after the camera is displayed, which causes the preview after an image is captured to be black
            if (source == UIImagePickerControllerSourceTypeCamera) {
                [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
            }
        }];
Dan
  • 468
  • 1
  • 4
  • 19
5

I found the same issue and tried everything. I have two different apps, one in objective-C and one in swift - both have the same problem. The error message comes in the debugger and the screen goes black after the first photo. This only happens in iOS >= 8.0, obviously it is a bug.

I found a difficult workaround. Shut off the camera controls with imagePicker.showsCameraControls = false and create your own overlayView that has the missing buttons. There are various tutorials around how to do this. The strange error message stays, but at least the screen doesn't go black and you have a working app.

tzer
  • 280
  • 4
  • 11
5

This might be a bug of built-in ImagePickerController. My code is working, but occasionally crashes on iPhone 6 Plus.

I've tried all solutions suggested by other answers but there were no luck. Problem finally solved after switching to JPSImagePickerController.

JonSlowCN
  • 318
  • 3
  • 11
3

I've tried everything, my problem was that the image picker for the camera and photo library disappeared right after they showed. I solved it with the following line (swift)

imagePicker.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
Waffeln
  • 213
  • 2
  • 7
3

I'm pretty sure this is just a bug in iOS 8.0. It's reproducible with the simplest of POC apps that does nothing more than attempt to present a UIImagePickerController like you're doing above. Furthermore, there's no alternative pattern to displaying the image picker/camera, to my knowledge. You can even download Apple's Using UIImagePickerController sample app, run it, and it will generate the same error out of the box.

That said, the functionality still works for me. Other than the warning/error, do you have issues with the functioning of your app?

Gaurav Patel
  • 532
  • 1
  • 5
  • 19
3

If we are using the UIImagePickerController as a property, then this warning will disappear. assume that we are not using the result from the UIImagePickerController , if we are instantiating the UIImagePickerController within a function.

NSNoob
  • 5,548
  • 6
  • 41
  • 54
Antony Ouseph
  • 552
  • 4
  • 10
2

Calling this method worked for me. Place it after presenting your view.

[yourViewBeingPresented.view layoutIfNeeded];
NSNoob
  • 5,548
  • 6
  • 41
  • 54
Bobby
  • 6,115
  • 4
  • 35
  • 36
1

I also encounter the same problem and I resolved it by checking if the camera is available:

BOOL cameraAvailableFlag = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
    if (cameraAvailableFlag)
        [self performSelector:@selector(showcamera) withObject:nil afterDelay:0.3];
AFTAB MUHAMMED KHAN
  • 2,189
  • 3
  • 18
  • 24
  • 1
    isSourceTypeAvailable returns true even though I've denied access to the camera for my app. The display is blank if the camera access is denied. – Καrτhικ Feb 14 '15 at 19:27
1

I have came across with this issue. When we call the camera and release the views produced this issue. For an example call an camera and set view nil in viewDidDisappear method this error will come since there is not callback for camera event. Make sure about this case too for this error.

Gobi M
  • 3,243
  • 5
  • 32
  • 47
1

I got the same bug,getting bellow message in console while opening camera.

'Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.'

For me problem was with the Bundle display name in Info.plist file.it was empty some how,i put my app name there and now it working fine.i did't received any camera permission alert because of empty Bundle display name.it blocked the view from rendering.

the problem was't with view but by presenting it without a permission.you can check it on settings-->privacy-->Camera,if your app not listed there problem might be same.

Shabeer Ali
  • 903
  • 11
  • 20
1

I'm using Phonegap, but this thread keeps coming as the first one when Googling about the error message.

For me this issue went away by defining the imagetype to PNG.

encodingType : Camera.EncodingType.PNG

So the whole line being:

 navigator.camera.getPicture(successFunction, failFunction, { encodingType : Camera.EncodingType.PNG, correctOrientation:true, sourceType : Camera.PictureSourceType    .PHOTOLIBRARY, quality: 70, allowEdit : false , destinationType: Camera.DestinationType.DATA_URL});

Your mileage may vary, but that did the trick for me.

Jannunen
  • 344
  • 5
  • 9
1

Alternatively, consider using drawViewHierarchyInRect:

Swift:

extension UIImage{

    class func renderUIViewToImage(viewToBeRendered: UIView) -> UIImage
    {
        UIGraphicsBeginImageContextWithOptions(viewToBeRendered.bounds.size, true, 0.0)
        viewToBeRendered.drawViewHierarchyInRect(viewToBeRendered.bounds, afterScreenUpdates: true)
        viewToBeRendered.layer.renderInContext(UIGraphicsGetCurrentContext()!)

        let finalImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return finalImage
    }
}

Objective-C:

- (UIImage *)snapshot:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0);
    [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

Also see:

Community
  • 1
  • 1
Eric
  • 16,003
  • 15
  • 87
  • 139
0

In my case ( XCode 7 and iOS 9 ), I use UINavigationController "hidden", so Ihave to add UINavigationControllerDelegate to present camera or roll and it work like it is supposed to! And pickerControllerDelegate.self doesn't display error either!

NSNoob
  • 5,548
  • 6
  • 41
  • 54
Daniel Arantes Loverde
  • 2,317
  • 2
  • 28
  • 41