当AVCaptureSession
的captureOutput
回调丢帧并抛出了OutOfBuffers
错误时,可以尝试以下解决方法:
AVCaptureVideoDataOutput
的alwaysDiscardsLateVideoFrames
属性为false
,以允许缓冲区重用:let videoOutput = AVCaptureVideoDataOutput()
videoOutput.alwaysDiscardsLateVideoFrames = false
AVCaptureSession
的sessionPreset
属性,以增加缓冲区的数量。例如,将其设置为AVCaptureSession.Preset.high
:let session = AVCaptureSession()
session.sessionPreset = AVCaptureSession.Preset.high
检查是否有其他可能导致性能问题的代码或操作。例如,确保没有过多的并发操作或资源消耗过高的处理逻辑。
如果需要处理大量数据帧,可以考虑使用dispatch_queue
来控制并发处理的数量。例如,可以使用DispatchQueue.concurrent
来创建一个并发队列,并在captureOutput
回调中使用async
方法来处理数据帧:
let videoOutputQueue = DispatchQueue(label: "com.example.videoOutputQueue", attributes: .concurrent)
videoOutput.setSampleBufferDelegate(self, queue: videoOutputQueue)
// ...
extension YourViewController: AVCaptureVideoDataOutputSampleBufferDelegate {
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
videoOutputQueue.async {
// 处理数据帧的代码
}
}
}
通过采取上述措施,您应该能够减少丢帧和解决OutOfBuffers
错误。