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

}];

任何想法?


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

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


我在这个问题上挣扎了几个小时,我阅读了每一个相关的主题,发现这个错误是因为我的设备的隐私设置下,我的应用程序的摄像头访问被阻止了!!我从来没有拒绝访问相机,我不知道它是如何被阻止的,但这就是问题所在!


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

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

我发现了同样的问题,并尝试了所有方法。我有两个不同的应用程序,一个在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];
    });
}

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

- (void)applicationWillResignActive:(UIApplication *)application

和YES

- (void)applicationDidBecomeActive:(UIApplication *)application

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


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

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


我遇到过这个问题。当我们调用相机并释放视图时就产生了这个问题。举个例子,调用一个相机并在viewDidDisappear方法中设置视图为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];
            }
        }];

I don't have enough reputation points to comment on @greg's answer above, so will add my observations here. I have a Swift project for both iPad and iPhone. I have a method inside my main view controller (relevant bit below). When I test this on a phone, everything works properly and no warnings are generated. When I run it on an iPad, everything works properly but I see the warning about snapshotting the view. The interesting bit, however, is that when I run on an iPad without using the popover controller, everything works properly with no warning. Unfortunately, Apple mandates that the image picker must be used within a popover on iPad, if the camera is not being used.

    dispatch_async(dispatch_get_main_queue(), {
        let imagePicker: UIImagePickerController = UIImagePickerController();
        imagePicker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum;
        imagePicker.mediaTypes = [kUTTypeImage];
        imagePicker.allowsEditing = false;
        imagePicker.delegate = self;

        if(UIDevice.currentDevice().userInterfaceIdiom == .Pad){ // on a tablet, the image picker is supposed to be in a popover
            let popRect: CGRect = buttonRect;
            let popover: UIPopoverController = UIPopoverController(contentViewController: imagePicker);
            popover.presentPopoverFromRect(popRect, inView: self.view, permittedArrowDirections: UIPopoverArrowDirection.Up, animated: true);
        }else{
            self.presentViewController(imagePicker, animated: true, completion: nil);
        }
    });

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

imagePicker.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext

我得到了同样的错误,在控制台怒吼的消息,同时打开相机。

’对一个没有被渲染的视图进行快照会导致一个空快照。确保你的视图在快照之前或屏幕更新后至少渲染过一次。”

对我来说,问题是在信息中的捆绑显示名称。plist文件。它是空的某种方式,我把我的应用程序名称在那里,现在它工作正常。我没有收到任何相机权限警报,因为空的Bundle显示名称。它阻止了视图的呈现。

问题不在于视图,而在于未经许可而呈现视图。你可以检查设置>隐私>相机,如果你的应用程序没有列出,问题可能是一样的。


我使用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});

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


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

[yourViewBeingPresented.view layoutIfNeeded];

我有一个UIAlertControllerStyleActionSheet给用户一个选项,用相机拍照或使用一个从库。

我在错误消息上尝试了一个符号断点

这表明错误是由演示期间实习生使用UICollectionView产生的

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

我通过在呈现前明确设置框架来解决这个问题

[alert setPreferredContentSize: alert.view.frame.size];

这里是完整的方法,是工作没有错误

-(void)showImageSourceAlertFromSender:(id)sender{
UIButton *senderButton = (UIButton*)sender;
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"Camera" style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction *action) {
                                                         [self takePhoto];
                                                     }];
UIAlertAction *libraryAction = [UIAlertAction actionWithTitle:@"Library" style:UIAlertActionStyleDefault
                                                      handler:^(UIAlertAction *action) {
                                                          [self selectPhotoFromLibraryFromSender:sender];
                                                      }];
[alert addAction:cameraAction];
[alert addAction:libraryAction];
alert.popoverPresentationController.delegate = self;
alert.popoverPresentationController.sourceRect = senderButton.frame;
alert.popoverPresentationController.sourceView = self.view;

[alert setPreferredContentSize: alert.view.frame.size];

[self presentViewController:alert animated:YES completion:^(){
}];}

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

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


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

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


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


如果我们使用UIImagePickerController作为属性,那么这个警告将会消失。xcode假设我们没有使用UIImagePickerController的结果,如果我们在一个函数中实例化UIImagePickerController。