在iOS 8中,我有从相机捕捉图像的问题,直到现在我正在使用这段代码

UIImagePickerController *controller=[[UIImagePickerController alloc] init];
controller.videoQuality=UIImagePickerControllerQualityTypeMedium;
controller.delegate=(id)self;
controller.sourceType=UIImagePickerControllerSourceTypeCamera;
[self presentViewController:controller animated:YES completion:nil];

但在iOS 8中,我得到了这个:

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.

我已经尝试了本文提供的解决方案

@property (strong,nonatomic)UIImagePickerController *controller;

_controller=[[UIImagePickerController alloc] init];
_controller.videoQuality=UIImagePickerControllerQualityTypeMedium;
_controller.delegate=(id)self;
_controller.sourceType=UIImagePickerControllerSourceTypeCamera;
_[self presentViewController:controller animated:YES completion:nil];

...
controller.modalPresentationStyle=UIModalPresentationFullScreen;
or
controller.modalPresentationStyle=UIModalPresentationCurrentContext;
...

double delayInSeconds = 0.1;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    [self presentViewController:controller animated:YES completion:nil];
});

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

[self presentViewController:controller animated:YES completion:^{

}];

任何想法?


当前回答

调用这个方法对我来说很有效。在陈述你的观点之后把它放上去。

[yourViewBeingPresented.view layoutIfNeeded];

其他回答

在呈现视图控制器之前,你可以通过引用视图属性来禁用“快照视图”警告。这样做会导致视图加载,并允许iOS在拍摄快照之前渲染它。

UIAlertController *controller = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
controller.modalPresentationStyle = UIModalPresentationPopover;
controller.popoverPresentationController.barButtonItem = (UIBarButtonItem *)sender;

... setup the UIAlertController ... 

[controller view]; // <--- Add to silence the warning.

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

我很确定这只是iOS 8.0的一个bug。这是最简单的POC应用程序的可复制性,它只是尝试呈现一个UIImagePickerController,就像你上面做的那样。此外,据我所知,没有其他模式可以显示图像拾取器/相机。你甚至可以下载苹果的Using UIImagePickerController示例应用程序,运行它,它会立即生成相同的错误。

也就是说,功能对我来说仍然有效。除了警告/错误,你的应用程序的功能有问题吗?

我在调用UIImagePickerController presentViewController后遇到了这个:从回调到UIAlertView委托。我通过推presentViewController解决了这个问题:使用dispatch_async调用当前执行跟踪。

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    dispatch_async(dispatch_get_main_queue(), ^{
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;

        if (buttonIndex == 1)
            imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        else
            imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;

        [self presentViewController: imagePickerController
                           animated: YES
                         completion: nil];
    });
}

我有这个问题时,动画一些视图和应用程序会进入背景模式,然后回来。我通过设置标志isActive来处理它。我把它设置为NO in

- (void)applicationWillResignActive:(UIApplication *)application

和YES

- (void)applicationDidBecomeActive:(UIApplication *)application

并相应地激活或不激活我的观点。解决了这个问题。

我很确定这只是iOS 8.0的一个bug。这是最简单的POC应用程序的可复制性,它只是尝试呈现一个UIImagePickerController,就像你上面做的那样。此外,据我所知,没有其他模式可以显示图像拾取器/相机。你甚至可以下载苹果的Using UIImagePickerController示例应用程序,运行它,它会立即生成相同的错误。

也就是说,功能对我来说仍然有效。除了警告/错误,你的应用程序的功能有问题吗?