Search code examples
iosiphoneios7camerauiimagepickercontroller

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

  • 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];
    }