UIImagePickerController 错误:对尚未呈现的视图进行快照会导致 iOS 7 中的快照为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18890003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:21:45  来源:igfitidea点击:

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

iosiphoneios7camerauiimagepickercontroller

提问by Didats Triadi

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.

我仅在 iOS 7 中收到此错误并且应用程序崩溃。在 iOS 6 中,我从来没有收到任何错误,只有一次打开相机时的内存警告。

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.

我确实试图延迟presentModalViewController,但我仍然收到相同的消息。几秒钟后(7-10),应用程序崩溃了。

This error is only present in iOS 7.

此错误仅出现在 iOS 7 中。

Anybody has the clue?

有人有线索吗?

采纳答案by Lefteris

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.

iOS7 中的问题与转换有关。似乎如果之前的转换没有完成而您启动了一个新的转换,iOS7 会混淆视图,而 iOS6 似乎可以正确管理它。

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

您应该在您的 中初始化您的相机UIViewController,只有在视图已加载并超时后:

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

回答by Scott Carter

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

这个错误也出现在我的 Apple 的 PhotoPicker 示例代码项目中。

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

我在 iPhone 4 上使用 Xcode 5.0 版和 iOS 7.0.3。

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.

  1. https://developer.apple.com/library/ios/samplecode/PhotoPicker/Introduction/Intro.html下载 Apple 的 PhotoPicker 示例项目

  2. 在 APLViewController.m 中注释掉第 125 行

    //imagePickerController.showsCameraControls = NO;

  3. 在 APLViewController.m 中注释掉第 130-133 行

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

  4. 构建并启动应用程序。

  5. 启动后,将设备旋转到横向模式。

  6. 单击相机图标以在相机模式下打开 UIImagePickerController。

  7. 查看控制台输出。

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.

PhotoPicker[240:60b] 对尚未渲染的视图进行快照会生成空快照。确保您的视图在屏幕更新之前或快照之前至少渲染过一次。

showsCameraControls property

显示CameraControls 属性

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

当它的值为 YES(默认值)时,我就会出现问题。

Setting this to NO eliminated the message.

将此设置为 NO 消除了该消息。

Bug report

错误报告

I just filed a bug report with Apple.

我刚刚向 Apple 提交了错误报告。

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

我已经尝试了在不同帖子中提出的许多建议,但没有找到令人满意的解决方法。

回答by Arne

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

当我尝试在弹出框内显示相机视图时遇到了问题。在 iOS6 下这没问题,但在 iOS7 中我收到消息

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 Libraryeverything 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 *) pickerwas called.

但是,在我按照拍摄照片和电影中的描述将相机视图的显示更改为全屏后,iOS 开发人员库一切正常,并且该消息再也没有出现过。但是,我必须确保根据应用程序处于哪种模式(即,呈现相机视图或照片胶卷),无论何时- (void) imagePickerControllerDidCancel: (UIImagePickerController *) picker调用该方法,我都必须关闭弹出窗口或视图控制器。

回答by Asfanur

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

这应该可以解决问题

回答by Germán Marquès

I used this code to workaround the problem:

我使用此代码来解决此问题:

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

if ([self respondsToSelector:@selector(presentViewController:animated:completion:)]){
    [imagePicker setShowsCameraControls:NO];
    [self presentViewController:imagePicker animated:YES completion:^{
        [imagePicker setShowsCameraControls:YES];
    }];
} else {
    [imagePicker setShowsCameraControls:YES];
    [self presentModalViewController:imagePicker animated:YES];
}

回答by Tony

I had the same issue when I was trying to modify the demo app that come with the Avirary SDK, in the demo app, it can only edit the photo picked from the camera roll. To try to edit the photo by capturing from camera, I first added the following code in the UIViewcontroller.m file:

当我尝试修改Avirary SDK附带的演示应用程序时,我遇到了同样的问题,在演示应用程序中,它只能编辑从相机胶卷中挑选的照片。为了尝试通过从相机捕获来编辑照片,我首先在 UIViewcontroller.m 文件中添加了以下代码:

#pragma mark - Take Picture from Camera
- (void)showCamera
{
//[self launchPhotoEditorWithImage:sampleImage highResolutionImage:nil];

    if ([self hasValidAPIKey]) {
        UIImagePickerController * imagePicker = [UIImagePickerController new];
        [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
        [imagePicker setDelegate:self];
        [imagePicker setAllowsEditing:YES]; //important, must have

        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
            [self presentViewController:imagePicker animated:YES completion:nil];
        }else{
            [self presentViewControllerInPopover:imagePicker];
        }
    }
}

Then when I run the app, the error occurred:

然后当我运行应用程序时,出现错误:

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.

To solve the error, modified the UIImagePicker delegate in your UIViewContooler.m file as shown below:

为了解决这个错误,修改了 UIViewContooler.m 文件中的 UIImagePicker 委托,如下所示:

#pragma mark - UIImagePicker Delegate

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSURL * assetURL = [info objectForKey:UIImagePickerControllerReferenceURL];

    void(^completion)(void)  = ^(void){

        [[self assetLibrary] assetForURL:assetURL resultBlock:^(ALAsset *asset) {
            if (asset){
                [self launchEditorWithAsset:asset];
            }
        } failureBlock:^(NSError *error) {
            [[[UIAlertView alloc] initWithTitle:@"Error" message:@"Please enable access to your device's photos." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
        }];

        UIImage * editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
        if(editedImage){
            [self launchPhotoEditorWithImage:editedImage highResolutionImage:editedImage];
        }

    };

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        [self dismissViewControllerAnimated:YES completion:completion];
    }else{
        [self dismissPopoverWithCompletion:completion];
    }

}    

Then the error disappeared and the app works!

然后错误消失了,应用程序运行了!

回答by Grigori A.

I get this error when building app with iOS SDK 6.1, deployment target iOS 6.1 and running app on iOS 7 powered iPhone. App doesn't crash but implementing UIViewController shouldAutorotatemethod helps me to remove error message.

使用 iOS SDK 6.1 构建应用程序、部署目标为 iOS 6.1 并在支持 iOS 7 的 iPhone 上运行应用程序时出现此错误。应用程序不会崩溃,但实现UIViewController shouldAutorotate方法可以帮助我删除错误消息。

- (BOOL)shouldAutorotate {
    return YES;
}

回答by Roman Ermolov

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:

我有同样的问题并找到了解决方法。我认为,该错误与您的应用程序的方向有关。我的应用程序仅使用横向模式,但 UIImagePickerController 使用纵向模式。我try-catch块添加到main.m文件,并获得真正的例外:

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.

1) 在 Target->General 或 .plist 文件中重新检查设备方向:支持的界面方向:横向左,横向右。

2) Add in AppDelegate.m:

2) 在 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:

在这一步之后 UIImagePickerController 工作正常,但我的视图控制器可以旋转到纵向模式。所以,要解决这个问题:

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

3)为UINavigationController创建一个类别,(在iOS6中supportedInterfaceOrientations从UIViewController移到UINavigationController):

@implementation UINavigationController (RotationIOS6)

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

@end

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

此解决方案适用于 iOS 6.0、6.1、7.0。希望这可以帮助。

回答by JerryZhou

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 imagePickeras a global variable.

取代。[self presentModalViewController:imagePicker animated:YES];和原因 makeimagePicker作为一个全局变量。

回答by user1045302

This is what fixed it for me on my app, ymmv

这是在我的应用程序 ymmv 上为我修复的

first off it is a iPhone - iPad app

首先它是一个 iPhone - iPad 应用程序

in appname-Info.plist. in the Supported interface orientations(iPad) showed 4 orientations.

在 appname-Info.plist 中。在支持的界面方向(iPad)中显示了 4 个方向。

in the Supported interface orientations showed 3 orientations. I added the fourth and ran the app, no debug output.

在支持的界面方向中显示了 3 个方向。我添加了第四个并运行了应用程序,没有调试输出。

Hope this helps.

希望这可以帮助。