UIImagePickerController error: Snapshotting a view that has not been rendered results in an empty snapshot in iOS 7

I am getting this error only in iOS 7 and the application crashed. In iOS 6, I never get any error, just once of memory warning when opening the 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.

Here is what I am doing.

imagePicker = [[UIImagePickerController alloc] init];
[imagePicker setDelegate:self];
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[imagePicker setAllowsEditing:YES];

[self presentModalViewController:imagePicker animated:YES];

I did tried to delay the presentModalViewController, but I am still getting the same message. After few seconds (7-10), the application crashed.

This error is only present in iOS 7.

Anybody has the clue?


Solution 1:

The problem in iOS7 has to do with transitions. It seems that if a previous transition didn't complete and you launch a new one, iOS7 messes the views, where iOS6 seems to manage it correctly.

You should initialize your Camera in your UIViewController, only after the view has Loaded and with a timeout:

- (void)viewDidAppear:(BOOL)animated 
{
    [super viewDidAppear:animated];
    //show camera...
    if (!hasLoadedCamera)
        [self performSelector:@selector(showcamera) withObject:nil afterDelay:0.3];
}

and here is the initialization code

- (void)showcamera {
    imagePicker = [[UIImagePickerController alloc] init];
    [imagePicker setDelegate:self];
    [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
    [imagePicker setAllowsEditing:YES];

    [self presentModalViewController:imagePicker animated:YES];
}

Solution 2:

This error also showed up for me with Apple's PhotoPicker sample code project.

I was using Xcode Version 5.0 and iOS 7.0.3 on an iPhone 4.

Steps to Reproduce:

  1. Download Apple's PhotoPicker sample project at https://developer.apple.com/library/ios/samplecode/PhotoPicker/Introduction/Intro.html

  2. In APLViewController.m comment out line 125

    //imagePickerController.showsCameraControls = NO;

  3. In APLViewController.m comment out lines 130-133

    //[[NSBundle mainBundle] loadNibNamed:@"OverlayView" owner:self options:nil];
    // self.overlayView.frame = imagePickerController.cameraOverlayView.frame;
    // imagePickerController.cameraOverlayView = self.overlayView;
    // self.overlayView = nil;

  4. Build and launch the app.

  5. Once launched, rotate device to Landscape mode.

  6. Click Camera icon to open UIImagePickerController in Camera mode.

  7. View the console output.

Console output

PhotoPicker[240:60b] 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.

showsCameraControls property

The problem occurs for me when this has a value of YES (the default).

Setting this to NO eliminated the message.

Bug report

I just filed a bug report with Apple.

I've tried many of the suggestions that have been made in different posts, but have not found a satisfactory workaround.

Solution 3:

I got the problem when I tried to present the camera view inside of a popover. Under iOS6 this was no problem but in iOS7 I got the message

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.

as well.

However after I changed the presentation of the camera view to fullscreen as described in Taking Pictures and Movies, iOS Developer Library everything went fine again and the message never appeared again. However I had to make sure that depending on in which mode the app is (i.e., presenting camera view or photo roll) I had to either dismiss the popover or the view controller whenever the method - (void) imagePickerControllerDidCancel: (UIImagePickerController *) picker was called.

Solution 4:

create a property

@property (nonatomic) UIImagePickerController *imagePickerController;

Then

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.modalPresentationStyle = UIModalPresentationCurrentContext;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.allowsEditing = YES;
self.imagePickerController = picker;
[self presentViewController:self.imagePickerController animated:YES completion:nil];

This should solve the problem