【问题标题】:AVCaptureSession canAddInput for front-camera always returns false on iPad前置摄像头的 AVCaptureSession canAddInput 在 iPad 上总是返回 false
【发布时间】:2020-09-17 14:25:58
【问题描述】:

以下代码在 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");

}

【问题讨论】:

    标签: ios objective-c xcode ipad avcapturesession


    【解决方案1】:

    很可能,它失败了,因为您将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

    【讨论】:

    • 谢谢哥们,帮帮我!我是 AV 新手,不知道这个。
    【解决方案2】:

    我遇到了另一个选项,我想在这里分享。这个问题并非如此,但它可能对某人有所帮助。

    我使用的是AVCaptureMulticamSession,但它无法在 iPhone X 和更早版本上运行:我收到一个错误,即会话无法添加设备输入。

    原因是 AVCaptureMulticamSession 仅在新设备​​上受支持,从 iPhone XR、iPhone XS、iPhone XS Max 和 iPad Pro(第 3 代)开始。可以通过isMultiCamSupported查看当前设备是否支持。

    【讨论】:

      猜你喜欢
      • 2013-06-08
      • 2015-10-31
      • 2018-08-21
      • 1970-01-01
      • 2014-11-16
      • 1970-01-01
      • 1970-01-01
      • 2012-02-19
      • 1970-01-01
      相关资源
      最近更新 更多