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

}];

任何想法?


当前回答

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

imagePicker.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext

其他回答

我也遇到了同样的问题,我通过检查相机是否可用来解决它:

BOOL cameraAvailableFlag = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
    if (cameraAvailableFlag)
        [self performSelector:@selector(showcamera) withObject:nil afterDelay:0.3];

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

- (void)applicationWillResignActive:(UIApplication *)application

和YES

- (void)applicationDidBecomeActive:(UIApplication *)application

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

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

imagePicker.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext

或者,考虑使用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而不损失视网膜显示的质量

在我的情况下(XCode 7和iOS 9),我使用UINavigationController“隐藏”,所以我必须添加UINavigationControllerDelegate来呈现相机或滚动,它的工作就像它应该做的!和pickerControllerDelegate。Self也不显示错误!