iOS 8快照未呈现的视图会导致一个空的快照

在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. 

我已经尝试过与本帖提供的解决scheme

 @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中的一个错误。 它可以与最简单的POC应用程序一起重现,除了像上面所做的那样尝试呈现UIImagePickerController , 此外,就我所知,没有其他的图像select器/相机显示模式。 你甚至可以下载苹果的使用UIImagePickerController示例应用程序 ,运行它,它会产生相同的错误框。

也就是说,function仍然适用于我。 除了警告/错误之外,您的应用程序的function是否有问题?

我在这个问题上挣扎了几个小时,我已经阅读了所有相关主题,发现错误是由于我的设备的隐私设置下,我的应用程序的相机访问被阻止! 我从来没有拒绝访问相机,我不知道它是如何被封锁,但这是问题!

我没有足够的声望点评论上面的@ greg的答案,所以在这里添加我的观察。 我有一个iPad和iPhone的Swift项目。 我有一个方法在我的主视图控制器(相关位下面)。 当我在手机上testing这一切,一切工作正常,没有警告产生。 当我在iPad上运行它时,一切正常,但我看到了有关快照视图的警告。 有趣的是,当我在iPad上运行而不使用popover控制器时,一切正常,没有任何警告。 不幸的是,苹果强制要求,如果相机没有被使用,图像select器必须在iPad的popup窗口中使用。

  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); } }); 

我调用UIImagePickerController presentViewController后:从callback到UIAlertView委托,我碰到这个。 我通过推送presentViewController来解决这个问题:使用dispatch_asyncclosures当前的执行轨迹。

 - (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]; }); } 

animation的一些意见和应用程序将进入后台模式,并回来时,我有这个问题。 我通过设置一个标志isActive来处理它。 我把它设置为NO

 - (void)applicationWillResignActive:(UIApplication *)application 

和YES

 - (void)applicationDidBecomeActive:(UIApplication *)application 

并据此animation或不animation我的观点。 照顾这个问题。

我有一个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:^(){ }];} 

在呈现视图控制器之前,您可以通过引用view属性来消除“快照视图”警告。 这样做会导致视图加载并允许iOS在创build快照之前进行渲染。

 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]; 

对于任何在图像捕获后看到黑色预览问题的人,在显示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]; } }]; 

我发现了同样的问题,并尝试了一切。 我有两个不同的应用程序,一个在Objective-C和一个在迅速 – 都有同样的问题。 错误信息出现在debugging器中,第一张照片后屏幕变黑。 这只发生在iOS> = 8.0,显然这是一个错误。

我发现一个困难的解决方法。 使用imagePicker.showsCameraControls = falseclosures相机控件,并创build自己的overlayView,其中包含缺less的button。 有关于如何做到这一点的各种教程。 奇怪的错误消息保持,但至less屏幕不会变黑,你有一个工作的应用程序。

这可能是一个内置的ImagePickerController的错误。 我的代码正在工作,但偶尔会在iPhone 6 Plus上崩溃。

我已经尝试了其他答案build议的所有解决scheme,但没有运气。 切换到JPSImagePickerController后问题终于解决了。

我试过了所有的东西,我的问题是相机和照片库的图像select器在显示后就消失了。 我用下面的行解决了它(swift)

 imagePicker.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext 

我很确定这只是iOS 8.0中的一个错误。 它是可重现的最简单的POC应用程序,只不过是尝试呈现一个UIImagePickerController,就像你在上面做的那样。 此外,就我所知,没有其他的图像select器/相机显示模式。 你甚至可以下载苹果的使用UIImagePickerController示例应用程序,运行它,它会产生相同的错误框。

也就是说,function仍然适用于我。 除了警告/错误之外,您的应用程序的function是否有问题?

调用这个方法对我有用。 展示您的观点后放置它。

 [yourViewBeingPresented.view layoutIfNeeded]; 

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

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

我遇到了这个问题。 当我们打电话给相机,并释放这个问题产生的意见。 例如,调用摄像头并在viewDidDisappear方法中设置视图nil,由于摄像头事件没有callback,因此会出现此错误。 请确保这种情况也是这个错误。

我得到了同样的错误,在打开相机时在控制台中获取波纹pipe信息。

“对未呈现的视图进行快照会导致空的快照。 确保您的视图在屏幕更新后的快照或快照之前至less已呈现一次。

对我来说,问题是在Info.plist文件中的Bundle显示名称。它是空的一些如何,我把我的应用程序的名称,现在它工作fine.i没有收到任何摄像头权限警报,因为空Bundle显示名称。阻止了渲染视图。

这个问题是不是看法,但通过提交没有权限。你可以检查设置 – >隐私 – >相机,如果你的应用程序没有列出那里的问题可能是相同的。

我正在使用Phonegap,但是在search错误消息时,这个线程继续作为第一个。

对于我来说,这个问题通过将图像types定义为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}); 

你的里程可能会有所不同,但这对我来说是个诡计。

另外,考虑使用drawViewHierarchyInRect

迅速:

 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; } 

另请参阅:

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

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