ios - 前置摄像头的 AVCaptureSession canAddInput 在 iPad 上总是返回 false

标签 ios objective-c xcode ipad 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");

}

最佳答案

最有可能的是,它失败是因为您将 sessionPreset 设置为 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

https://stackoverflow.com/a/53214766/3338129

关于ios - 前置摄像头的 AVCaptureSession canAddInput 在 iPad 上总是返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62095755/

相关文章:

javascript - 在 React Native 中需要图像模块时遇到问题

ios - 未调用 ContentBlockerRequestHandler 类?

ios - 如何获取UIImage的dpi/ppi?

objective-c - 是否有任何私有(private) api 来监控 iPhone 上的网络流量?

ios - 如何将类属性与枚举匹配?

objective-c - 如何让按钮在单击时显示一个窗口?

ios - 在从 Xcode 启动模拟器或设备之前删除应用程序

ios - 如何在 Xcode 8.1 中修复此错误(返回 host_statistics)?我想知道设备的可用内存

ios - 中止陷阱 : 6 error in the new Swift 3. 1/Xcode 8.3

ios - 如何在 iOS 上使用 BLE(低功耗蓝牙)从人体秤获取数据作为 CBPeripheral