iOS 7 UIImagePickerController相机无图像 [英] iOS 7 UIImagePickerController Camera No Image

查看:139
本文介绍了iOS 7 UIImagePickerController相机无图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

For some reason the first time I open the UIImagePickerController in camera mode on my app it comes up blank. I have to close and reopen that view to get the camera feed to start working. I'm using the standard code that works in iOS 6 perfectly for camera capture. From the sample below I'm firing the capturePhoto: method. Anyone else running into this jenkiness with the iOS 7 camera? I checked the Apple dev forums but its near impossible to find answers there.

- (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授权的内容。

I'm also using UIImagePickerController and ran into the same issue with a blank screen. I'd like to expand a little on what klaudz mentioned regarding iOS 7 authorization for the camera.

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

录制音频始终需要用户的明确许可;录制视频还需要用户在某些地区销售的设备的权限。

"Recording audio always requires explicit permission from the user; recording video also requires user permission on devices sold in certain regions."

以下是一些代码片段,您可以查看是否拥有相机的权限,并在您的应用之前没有请求时请求它。如果您因先前的请求而被拒绝,您的应用可能需要向用户发出通知,进入设置以手动启用访问权限,正如klaudz指出的那样。

Here is some code fragments you can start with to check to see if you have permission for the camera and request it if your app hadn't previously requested it. If you are denied due to an earlier request, your app may need to put up a notice to the user to go into settings to manually enable access as klaudz pointed out.

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 UIImagePickerController相机无图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆