设置AVCaptureDevice格式在特定实例中不起作用

时间:2019-06-14 15:01:07

标签: ios swift avfoundation

作为iOS应用程序的一部分,用户使用2种不同的ViewController,它们具有不同的AVCaptureDevice格式(不同的分辨率,帧速率等)以及可视化预览。该应用程序始终使用NavigationController。用户将应用程序打开到主屏幕,然后移动到带有摄像机预览的第一个ViewController,然后移动到ViewControllers以使用带有摄像机预览的第二个ViewController。第二台摄像机ViewController(VC)录制视频。

如果用户完成了从主屏幕到第二摄像机VC的整个流程,然后又返回主屏幕,则不会有问题。

但是,如果用户通过第二个摄像机VC进行录制视频,然后从第一个摄像机弹出到主屏幕,然后重复,则尝试在第二个VC上录制视频,即AVCaptureDevice的activeFormat尽管已在代码中明确设置,但设置不正确!有趣的是,activeFormat如果在第二行(1行之后)进行了正确设置,则设置正确。去搞清楚。

我认为这可能与NavigationController有关,并且发生配置的地方,即viewDidLoadviewWillAppearAVCaptureSession可能未正确结束?

在第一个VC中,摄像机配置被掩埋在第3方代码中。

在第二个VC中,这是摄像机配置代码:

var bestFormat: AVCaptureDevice.Format?

for format in device.formats {
    let dims = CMVideoFormatDescriptionGetDimensions(format.formatDescription)
    if dims.width * dims.height == x {
        for range in format.videoSupportedFrameRateRanges {
            if range.maxFrameRate == y {
                bestFormat = format
            }
        }
    }
}

// set format
do {
    try device.lockForConfiguration()
    if let bf = bestFormat {
        device.activeFormat = bf
        device.activeFormat = bf // here's where format could be set a second time to resolve the issue
    } else {
        print("bestFormat nil or bestFrameRateRange nil")
    }
    device.unlockForConfiguration()
} catch {
    print("Camera configuration error")
}

我希望有人可以指出我在这个方向上的正确方向。

0 个答案:

没有答案
相关问题