我有一个正在运行的 session 和一个显示在我 View 中的预览层。
我需要在我的应用程序中多次更改 AVCaptureStillImageOutput、AVCaptureMetadataOutput 和 AVCaptureVideoDataOutput 的输出,而我的预览应该显示流畅,不会闪烁。
问题:当我向该 session 添加输出时,预览会闪烁(请找到我附加的 gif)。
导致问题的具体行:
self.stillImageOutput = AVCaptureStillImageOutput()
self.stillImageOutput?.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
if session.canAddOutput(self.stillImageOutput) {
session.addOutput(self.stillImageOutput)
}
我的问题: 将输出添加到正在运行的 session 时,如何避免 AVCaptureVideoPreviewLayer 闪烁?
最佳答案
我在这个 article 中找到了适用于 iPhone 6 及更高版本的解决方案其中描述了如何在视频中捕获高分辨率静止图像:
New AV Foundation Camera Features for the iPhone 6 and iPhone 6 Plus
...
..capture full resolution still images without interrupting preview and reconfiguring the device.
解决方法:
我没有添加和删除输出,而是将所有输出添加到 session 中。
对于视频捕获,我在 session 预设中设置了所需的分辨率 (1280x720):
session.sessionPreset = AVCaptureSessionPreset1280x720
这一行的结果是捕获设备(在我的例子中是后置摄像头)的事件格式发生了变化。
每种格式都有 HRSI(高分辨率静止图像),它描述了高分辨率图像的尺寸。
This feature is off by default. To enable it, you call AVCaptureStillImageOutput setHighResolutionStillImageOutputEnabled:.
为了拍摄高分辨率静止图像,应将标志 highResolutionStillImageOutputEnabled 设置为 true:
self.stillImageOutput?.highResolutionStillImageOutputEnabled = true
以 iPhone 6 为例,静态图像分辨率为:3264x1836,相机预览不闪烁。
关于ios - 更改 AVCaptureOutput 时如何避免 AVCaptureVideoPreviewLayer 闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39504005/