0

我想从didFinishProcessingPhoto委托方法中获取一个像素缓冲区,但它是零。

func capturePhoto() {
        let format = [AVVideoCodecKey: AVVideoCodecType.jpeg]
        let settings = AVCapturePhotoSettings(format: format)
        output.capturePhoto(with: settings, delegate: self)
    }

和扩展:

extension CaptureSessionManager: AVCapturePhotoCaptureDelegate {
    func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
        guard let pixelBuffer = photo.pixelBuffer else {
            return
        }

        bufferSubject.onNext(pixelBuffer)
    }
}

在此之前,我显然是在向会话中添加输出。我应该使用与该委托不同的方法吗?

4

1 回答 1

0

我在阅读文档后自己弄清楚了。

首先确保AVCaptureSession.Preset.photo使用sessionPreset: session.sessionPreset = .photo

然后使用 rawPixeFormatType 很重要,因为将其设置为 jpeg 或 hevc 不会产生 pixelBuffer。可以这样实现:

guard let availableRawFormat = self.output.availableRawPhotoPixelFormatTypes.first else {
            return
        }

let photoSettings = AVCapturePhotoSettings(rawPixelFormatType: availableRawFormat,
                                                   processedFormat: [AVVideoCodecKey : AVVideoCodecType.hevc])

output.capturePhoto(with: photoSettings, delegate: self)

processedFormat:init是AVCapturePhotoSettings可选的。

于 2018-09-29T10:51:40.933 回答