iOS 7 UIImagePickerController相机没有图像

出于某种原因,我第一次在我的应用程序中以相机模式打开UIImagePickerController时,出现空白。 我必须closures并重新打开该视图才能使摄像头馈送开始工作。 我正在使用iOS 6中的标准代码来完美捕捉相机。 从下面的示例中我发射capturePhoto:方法。 其他任何人用iOS 7相机跑到这个沉闷的地方? 我检查了苹果开发论坛,但几乎不可能find答案。

 - (IBAction)capturePhoto:(id)sender { [self doImagePickerForType:UIImagePickerControllerSourceTypeCamera]; } - (void)doImagePickerForType:(UIImagePickerControllerSourceType)type { if (!_imagePicker) { _imagePicker = [[UIImagePickerController alloc] init]; _imagePicker.mediaTypes = @[(NSString*)kUTTypeImage]; _imagePicker.delegate = self; } _imagePicker.sourceType = type; [self presentViewController:_imagePicker animated:YES completion:nil]; } 

为什么这么空?

我也使用UIImagePickerController,并遇到空白屏幕相同的问题。 我想就klaudz提到的有关iOS 7授权的摄像头做一点点的介绍。

参考: https : //developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVCaptureDevice_Class/Reference/Reference.html

“录制audio总是需要用户的明确许可,录制video也需要用户在某些地区销售的设备上的许可。”

以下是一些代码片段,您可以先查看是否有摄像头的权限,如果您的应用程序以前没有请求过,请求它。 如果由于先前的请求而被拒绝,则您的应用可能需要向用户发出通知,以便进入手动启用访问的设置,如klaudz指出的那样。

iOS 7的例子

 NSString *mediaType = AVMediaTypeVideo; // Or AVMediaTypeAudio AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType]; // This status is normally not visible—the AVCaptureDevice class methods for discovering devices do not return devices the user is restricted from accessing. if(authStatus == AVAuthorizationStatusRestricted){ NSLog(@"Restricted"); } // The user has explicitly denied permission for media capture. else if(authStatus == AVAuthorizationStatusDenied){ NSLog(@"Denied"); } // The user has explicitly granted permission for media capture, or explicit user permission is not necessary for the media type in question. else if(authStatus == AVAuthorizationStatusAuthorized){ NSLog(@"Authorized"); } // Explicit user permission is required for media capture, but the user has not yet granted or denied such permission. else if(authStatus == AVAuthorizationStatusNotDetermined){ [AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) { // Make sure we execute our code on the main thread so we can update the UI immediately. // // See documentation for ABAddressBookRequestAccessWithCompletion where it says // "The completion handler is called on an arbitrary queue." // // Though there is no similar mention for requestAccessForMediaType, it appears it does // the same thing. // dispatch_async(dispatch_get_main_queue(), ^{ if(granted){ // UI updates as needed NSLog(@"Granted access to %@", mediaType); } else { // UI updates as needed NSLog(@"Not granted access to %@", mediaType); } }); }]; } else { NSLog(@"Unknown authorization status"); } 

在iOS 7中,应用程序可以在获得用户授权之前访问摄像头。 当应用首次访问摄像头时,iOS会显示一个警告视图来询问用户。 用户也可以在Settings--Privacy--Camera--[Your app's name]Settings--Privacy--Camera--[Your app's name]授权。

如果开关closures,相机将保持黑色的空白视图。

  1. 如果您使用AVCaptureDeviceInput调用相机,则可以检查如下内容:

     NSError *inputError = nil; AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&inputError]; if (inputError && inputError.code == AVErrorApplicationIsNotAuthorizedToUseDevice) { // not authorized } 
  2. 如果您使用UIImagePickerController调用,我仍然在寻找一种方法来检查是否获得授权。

    我试了这两种方法:

     [UIImagePickerController isSourceTypeAvailable:] [UIImagePickerController isCameraDeviceAvailable:] 

    但他们没有工作,他们都回来了。

UPDATE

感谢Scott的扩张。 [AVCaptureDevice authorizationStatusForMediaType:]是一个更好的方法来检查。

 AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; if (authStatus == AVAuthorizationStatusAuthorized) { // successful } else { // failed, such as // AVAuthorizationStatusNotDetermined // AVAuthorizationStatusRestricted // AVAuthorizationStatusNotDetermined } 

但请记住检查iOS版本,因为[AVCaptureDevice authorizationStatusForMediaType:]AVAuthorizationStatus在iOS 7上方可用。

 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) { // code for AVCaptureDevice auth checking } 

我遇到了完全相同的问题,并试图在互联网上的每一个解决scheme,没有运气。 但最后我发现这是后台线程阻止相机预览显示。 如果你像我一样尝试打开摄像头时碰巧有后台线程运行,请尝试阻止后台线程,看看会发生什么。 希望你能解决它。

我碰到这个控制AQPhotoPicker 。 这很容易使用,并希望它会帮助你