8

出于某种原因,当我第一次UIImagePickerController在我的应用程序上打开相机模式时,它会出现空白。我必须关闭并重新打开该视图才能让相机源开始工作。我正在使用适用于 iOS 6 的标准代码完美地进行相机捕捉。从下面的示例中,我正在触发该capturePhoto:方法。还有其他人在使用 iOS 7 相机时遇到这种问题吗?我检查了 Apple 开发论坛,但几乎不可能在那里找到答案。

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

为什么这么空?

4

4 回答 4

16

我也在使用 UIImagePickerController 并在空白屏幕上遇到了同样的问题。我想稍微扩展一下 klaudz 提到的有关 iOS 7 相机授权的内容。

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

“录制音频总是需要用户的明确许可;录制视频还需要用户对某些地区销售的设备的许可。”

下面是一些代码片段,您可以从这些代码片段开始检查您是否拥有摄像头权限,如果您的应用之前没有请求过,请请求它。如果您由于较早的请求而被拒绝,您的应用程序可能需要向用户发出通知以进入设置以手动启用访问权限,正如 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");
}
于 2013-09-29T02:55:49.357 回答
7

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

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

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

    但他们没有工作,他们都返回是。

更新

感谢 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
}
于 2013-09-23T08:58:48.487 回答
4

我遇到了完全相同的问题,并尝试了互联网上的所有解决方案,但没有运气。但最后我发现是后台线程阻止了相机预览出现。如果您在尝试像我一样打开相机时碰巧有后台线程运行,请尝试阻止后台线程并查看会发生什么。希望你能绕过它。

于 2013-09-27T23:56:23.333 回答
0

我遇到了这个控件AQPhotoPicker。使用起来很简单,希望对你有帮助

于 2014-09-14T11:36:56.567 回答