iPad 上前置摄像头的 AVCaptureSession canAddInput 始终返回 false

Swi*_*ude 3 xcode objective-c ipad ios avcapturesession

以下代码在 iPhone 上完美运行。在后置摄像头和前置摄像头之间来回切换。然而,当在 iPad 上运行时,选择前置摄像头时canAddInput- 方法总是返回(后置摄像头工作正常)。NO有什么想法吗?

- (void)addVideoInput:(BOOL)isFront{

    AVCaptureDevice *videoDevice;

    //NSLog(@"Adding Video input - front: %i", isFront);

    [self.captureSession removeInput:self.currentInput];

    if(isFront == YES){
        self.isFrontCam = YES;
        videoDevice = [self frontFacingCameraIfAvailable];
    }else{
        self.isFrontCam = NO;
        videoDevice = [self backCamera];
    }



    if (videoDevice) {

        NSError *error;
        AVCaptureDeviceInput *videoIn = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
        if (!error) {

          // Everything's fine up to here.
          // the next line always resolves to NO and thus the
          // Video input isn't added.



            if ([[self captureSession] canAddInput:videoIn]){
                [[self captureSession] addInput:videoIn];
                self.currentInput = videoIn;

                // Set the preset for the video input

                if([self.captureSession canSetSessionPreset:AVCaptureSessionPreset1920x1080]){
                    [self.captureSession setSessionPreset:AVCaptureSessionPreset1920x1080];
                }
            }
            else {
                NSLog(@"Couldn't add video input");
                NSLog(@"error: %@", error.localizedDescription);
            }
        }
        else{
            NSLog(@"Couldn't create video input");
            NSLog(@"error: %@", error.localizedDescription);
        }
    }else
        NSLog(@"Couldn't create video capture device");

}
Run Code Online (Sandbox Code Playgroud)

uni*_*b0y 6

最有可能的是,它失败了,因为您将sessionPreset1080p 设置为 1080p,而 iPad 的前置摄像头不是 1080p。

我遇到了同样的问题,只是将其设置为.high,根据 Apple 的定义,指定适合高质量视频和音频输出的捕获设置。它应该只选择任何相机支持的最高分辨率。

如果您不相信此描述,您还可以创建一系列您喜欢的预设,并在切换摄像机之前检查它们是否适用于您想要使用的摄像机。

我从基本相同问题的答案中获取了以下代码:

let videoPresets: [AVCaptureSession.Preset] = [.hd4K3840x2160, .hd1920x1080, .hd1280x720] //etc. Put them in order to "preferred" to "last preferred"
let preset = videoPresets.first(where: { device.supportsSessionPreset($0) }) ?? .hd1280x720
captureSession.sessionPreset = preset
Run Code Online (Sandbox Code Playgroud)

/sf/answers/3725033651/