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

}];

任何想法?


当前回答

对于任何人来说,在图像捕捉后看到黑色预览的问题,隐藏状态栏后显示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];
            }
        }];

其他回答

我使用Phonegap,但这个线程一直作为第一个当谷歌关于错误消息。

对我来说,通过将imagetype定义为PNG,这个问题就消失了。

encodingType : Camera.EncodingType.PNG

所以整条线是:

 navigator.camera.getPicture(successFunction, failFunction, { encodingType : Camera.EncodingType.PNG, correctOrientation:true, sourceType : Camera.PictureSourceType    .PHOTOLIBRARY, quality: 70, allowEdit : false , destinationType: Camera.DestinationType.DATA_URL});

你的里程可能会有所不同,但这对我来说很管用。

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

- (void)applicationWillResignActive:(UIApplication *)application

和YES

- (void)applicationDidBecomeActive:(UIApplication *)application

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

或者,考虑使用drawviewherarchyinrect:

迅速:

extension UIImage{

    class func renderUIViewToImage(viewToBeRendered: UIView) -> UIImage
    {
        UIGraphicsBeginImageContextWithOptions(viewToBeRendered.bounds.size, true, 0.0)
        viewToBeRendered.drawViewHierarchyInRect(viewToBeRendered.bounds, afterScreenUpdates: true)
        viewToBeRendered.layer.renderInContext(UIGraphicsGetCurrentContext()!)

        let finalImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return finalImage
    }
}

objective - c:

- (UIImage *)snapshot:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0);
    [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

还看到:

https://developer.apple.com/library/ios/qa/qa1817/_index.html 如何捕获UIView到UIImage而不损失视网膜显示的质量

这可能是内置ImagePickerController的错误。我的代码可以运行,但在iPhone 6 Plus上偶尔会崩溃。

我已经尝试了其他答案提出的所有解决方案,但都没有运气。问题最终解决后切换到JPSImagePickerController。

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