在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:^{

}];

任何想法?


当前回答

我发现了同样的问题,并尝试了所有方法。我有两个不同的应用程序,一个在objective-C和一个在swift -两者都有同样的问题。错误消息出现在调试器中,屏幕在第一张照片之后变黑。这只发生在iOS >= 8.0,显然这是一个错误。

我找到了一个困难的变通办法。用imagePicker关闭相机控制。showsCameraControls = false并创建自己的overlayView,其中有缺失的按钮。关于如何做到这一点,有各种各样的教程。 奇怪的错误信息保留了下来,但至少屏幕不会变黑,而且你有一个工作的应用程序。

其他回答

我在调用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];
    });
}

对于任何人来说,在图像捕捉后看到黑色预览的问题,隐藏状态栏后显示UIPickerController似乎可以解决这个问题。

UIImagePickerControllerSourceType source = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] ? UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypeSavedPhotosAlbum;
UIImagePickerController *cameraController = [[UIImagePickerController alloc] init];
        cameraController.delegate = self;
        cameraController.sourceType = source;
        cameraController.allowsEditing = YES;
        [self presentViewController:cameraController animated:YES completion:^{
            //iOS 8 bug.  the status bar will sometimes not be hidden after the camera is displayed, which causes the preview after an image is captured to be black
            if (source == UIImagePickerControllerSourceTypeCamera) {
                [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
            }
        }];

在呈现视图控制器之前,你可以通过引用视图属性来禁用“快照视图”警告。这样做会导致视图加载,并允许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];

我已经尝试了一切,我的问题是,相机和图片库的图像选择器在它们显示后立即消失。我用下面这行(swift)解决了它

imagePicker.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext

我遇到过这个问题。当我们调用相机并释放视图时就产生了这个问题。举个例子,调用一个相机并在viewDidDisappear方法中设置视图为nil,这个错误将会出现,因为没有相机事件的回调。对于这个错误,也要确认一下。