UIImagePickerController error: Snapshotting a view

2020-01-24 03:54发布

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? Thank you in advance.

16条回答
放荡不羁爱自由
2楼-- · 2020-01-24 04:08

Try this, use

[self performSelector:@selector(presentCameraView) withObject:nil afterDelay:1.0f];

and function

-(void)presentCameraView{
    [self presentViewController:imagePicker animated:YES completion:nil];
}

to replace. [self presentModalViewController:imagePicker animated:YES]; and of cause make imagePicker as a global variable.

查看更多
地球回转人心会变
3楼-- · 2020-01-24 04:08

In my case it was related with a layout change: the VC presenting the UIImagePickerViewController has the status bar hidden, but the UIImagePickerViewController hasn't.

So, I solved it hiding the status bar in the UIImagePickerViewController as it's shown in this answer.

查看更多
霸刀☆藐视天下
4楼-- · 2020-01-24 04:09

I spent long time try to find the solution, and surprisingly I have found it at the end and it was just very funny once I discovered it.

Here is what you will do to retrieve the image you picked and resume working :)

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
    UIImage* pickedImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    [composeImageView setImage:pickedImage];
[picker dismissViewControllerAnimated:YES completion:nil];
 }

Yes, to solve the issue, you only need to dismiss the picker normally as it seems this 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." stops the picker from being responsive but you can dismiss it and retrieve the image normally.

查看更多
姐就是有狂的资本
5楼-- · 2020-01-24 04:10

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

查看更多
趁早两清
6楼-- · 2020-01-24 04:13

I've just encountered the same issue. In my case the problem was that I had some non-ARC code and I've migrated it to ARC. When I did the migration, I didn't hold a strong reference to the UIImagePickerController and that was the reason for the crash.

Hope it helps :)

查看更多
放荡不羁爱自由
7楼-- · 2020-01-24 04:15

I have the same issue and found a solve. I think, that error related with orientation of your application. My application uses only landscape mode, but UIImagePickerController use portrait mode. I add try-catch block to main.m, and get real exception:

Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES

How i solve this problem:

1) Recheck device orientation in Target->General, or .plist file: Supported interface orientations : Landscape left, Landscape right.

2) Add in AppDelegate.m:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    return UIInterfaceOrientationMaskLandscape | UIInterfaceOrientationMaskPortrait;
}

After this step UIImagePickerController works properly, but my viewcontrollers can be rotated to portrait mode. So, to solve this:

3) Create a category for UINavigationController, (supportedInterfaceOrientations moved from UIViewController to UINavigationController in iOS6):

@implementation UINavigationController (RotationIOS6)

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

@end

This solution works properly on iOS 6.0, 6.1, 7.0. Hope this helps.

查看更多
登录 后发表回答