UIImagePickerController错误:在iOS 7中快照未呈现的视图会导致空快照

时间:2013-09-19 08:42:44

标签: ios iphone ios7 camera uiimagepickercontroller

我只在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.

这就是我在做什么。

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

[self presentModalViewController:imagePicker animated:YES];

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

此错误仅出现在iOS 7中。

有人有线索吗?提前谢谢。

16 个答案:

答案 0 :(得分:32)

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

只有在视图已加载且超时后,才应在UIViewController中初始化相机:

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

这是初始化代码

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

    [self presentModalViewController:imagePicker animated:YES];
}

答案 1 :(得分:18)

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

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

重现步骤:

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

  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. 查看控制台输出。

  8. 控制台输出

    PhotoPicker [240:60b]快照未渲染的视图会导致空快照。确保在屏幕更新后快照或快照之前,您的视图至少呈现过一次。

    showsCameraControls属性

    如果此值为YES(默认值),则会出现问题。

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

    错误报告

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

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

答案 2 :(得分:11)

当我尝试在弹出窗口中显示摄像机视图时,我遇到了问题。在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.

也是。

然而,在我按照Taking Pictures and Movies, iOS Developer Library所述将相机视图的显示更改为全屏后,一切都恢复正常,并且消息再也没有出现。但是我必须确保取决于应用程序在哪种模式下(即呈现相机视图或照片滚动),无论何时调用方法- (void) imagePickerControllerDidCancel: (UIImagePickerController *) picker,我都必须关闭popover或视图控制器。

答案 3 :(得分:6)

创建一个属性

@property (nonatomic) UIImagePickerController *imagePickerController;

然后

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

这应解决问题

答案 4 :(得分:4)

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

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

答案 5 :(得分:3)

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

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

我如何解决这个问题:

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

2)加入AppDelegate.m:

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

在此步骤之后,UIImagePickerController正常工作,但我的viewcontrollers可以旋转到纵向模式。所以,要解决这个问题:

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

@implementation UINavigationController (RotationIOS6)

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

@end

此解决方案适用于iOS 6.0,6.1,7.0。希望这会有所帮助。

答案 6 :(得分:3)

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

- (BOOL)shouldAutorotate {
    return YES;
}

答案 7 :(得分:3)

当我尝试修改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];
        }
    }
}

然后当我运行应用程序时,发生了错误:

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.

要解决此错误,请修改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];
    }

}    

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

答案 8 :(得分:1)

试试这个,使用

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

和功能

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

替换。 [self presentModalViewController:imagePicker animated:YES]; 因为将imagePicker作为全局变量。

答案 9 :(得分:1)

这是我在我的应用程序上修复它的原因,ymmv

首先它是iPhone - iPad应用程序

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

<支持界面中的

方向显示3个方向。我添加了第四个并运行了应用程序,没有调试输出。

希望这有帮助。

答案 10 :(得分:0)

我刚遇到同样的问题。在我的情况下,问题是我有一些非ARC代码,我已将其迁移到ARC。当我进行迁移时,我没有强烈引用UIImagePickerController,这就是崩溃的原因。

希望有所帮助:)

答案 11 :(得分:0)

我在iOS 8中遇到了同样的问题,但在设置 - &gt;内部,相机访问被禁用了我的应用程序的隐私。刚启用它,它正在工作。

答案 12 :(得分:0)

我花了很长时间试图找到解决方案,令人惊讶的是我最终找到了它,一旦我发现它就非常有趣。

您将采取以下措施来检索您选择并恢复工作的图像:)

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

是的,要解决此问题,您只需要正常关闭选择器,因为它似乎是这样的消息:&#34;快照未渲染的视图会导致空快照。确保您的视图在屏幕更新后的快照或快照之前至少呈现过一次。&#34;阻止选择器响应,但您可以解除它并正常检索图像。

答案 13 :(得分:0)

在我的情况下,它与布局更改有关:显示UIImagePickerViewController的VC隐藏了状态栏,但UIImagePickerViewController没有。

所以,我解决了隐藏UIImagePickerViewController状态栏的问题,因为它显示在answer中。

答案 14 :(得分:-1)

没有直接回答您的问题,但您提到您有内存警告,您可能将原始图像存储在可能导致内存警告的属性中。这是因为原始图像占用大约30MB的内存。在iPhone 4系列上测试iOS6上的应用程序时,我注意到了类似的内存警告。当设备升级到iOS7时,我仍然收到此警告。在iOS7上测试iPhone 5系列时没有内存警告。

答案 15 :(得分:-5)

更改

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

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

为我解决了这个问题。