8

何らかの理由で、アプリでカメラ内モードを初めて開いたときにUIImagePickerController空白になります。カメラ フィードを機能させるには、そのビューを閉じてから再度開く必要があります。私は、iOS 6 でカメラ キャプチャに完全に機能する標準コードを使用しています。以下のサンプルから、capturePhoto:メソッドを起動しています。iOS 7 のカメラでこの問題に遭遇した人は他にいますか? Apple dev フォーラムをチェックしましたが、そこで回答を見つけることはほぼ不可能です。

- (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 を使用しており、空白の画面で同じ問題に遭遇しました。カメラの iOS 7 認証に関して klaudz が言及したことを少し拡張したいと思います。

参照: 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、承認されたかどうかを確認する方法をまだ探しています。

    私はこれらの2つの方法を試しました:

    [UIImagePickerController isSourceTypeAvailable:]
    [UIImagePickerController isCameraDeviceAvailable:]
    

    しかし、全員が YES を返したため、うまくいきませんでした。

アップデート

スコットの拡大に​​感謝します。[AVCaptureDevice authorizationStatusForMediaType:]確認するより良い方法です。

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

ただし、[AVCaptureDevice authorizationStatusForMediaType:]AVAuthorizationStatusは iOS 7 以降で利用できるため、iOS のバージョンを確認することを忘れないでください。

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
    // code for AVCaptureDevice auth checking
}
于 2013-09-23T08:58:48.487 に答える