CMSampleBufferGetImageBuffer(sampleBuffer) return 无

CMSampleBufferGetImageBuffer(sampleBuffer) return nil

我使用此代码从相机捕获视频,但 CMSampleBufferGetImageBuffer(sampleBuffer) 始终 return 为零。问题是什么?。这是代码,我修改了这个来源的代码以适应 Swift 4 https://github.com/FlexMonkey/CoreImageHelpers/blob/master/CoreImageHelpers/coreImageHelpers/CameraCaptureHelper.swift

import AVFoundation
import CoreMedia
import CoreImage
import UIKit


class CameraCaptureHelper: NSObject
{
let captureSession = AVCaptureSession()
let cameraPosition: AVCaptureDevice.Position

weak var delegate: CameraCaptureHelperDelegate?

required init(cameraPosition: AVCaptureDevice.Position)
{
    self.cameraPosition = cameraPosition

    super.init()

    initialiseCaptureSession()
}

fileprivate func initialiseCaptureSession()
{
    captureSession.sessionPreset = AVCaptureSession.Preset.photo

    guard let camera = AVCaptureDevice.default(.builtInWideAngleCamera,
                                               for: .video, position: cameraPosition)
        else {
            fatalError("Unable to access camera")
    }
    do
    {
        let input = try AVCaptureDeviceInput(device: camera)

        captureSession.addInput(input)
    }
    catch
    {
        fatalError("Unable to access back camera")
    }

    let videoOutput = AVCaptureVideoDataOutput()

    videoOutput.setSampleBufferDelegate(self,
                                        queue: DispatchQueue(label: "sample buffer delegate", attributes: []))

    if captureSession.canAddOutput(videoOutput)
    {
        captureSession.addOutput(videoOutput)
    }

    captureSession.startRunning()
}
}

extension CameraCaptureHelper: AVCaptureVideoDataOutputSampleBufferDelegate
{

func captureOutput(_ output: AVCaptureOutput, didDrop sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {

    connection.videoOrientation = .landscapeRight //AVCaptureVideoOrientation(rawValue: UIApplication.shared.statusBarOrientation.rawValue)!

    guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else
    {
        return
    }

    DispatchQueue.main.async
        {
            self.delegate?.newCameraImage(self,
                                          image: CIImage(cvPixelBuffer: pixelBuffer))
    }
}

}

protocol CameraCaptureHelperDelegate: class
{
func newCameraImage(_ cameraCaptureHelper: CameraCaptureHelper, image: CIImage)
}

您正在尝试从 "just dropped a sample buffer" 回调访问像素缓冲区。头文件说:

CMSampleBuffer object passed to this delegate method will contain metadata about the dropped video frame, such as its duration and presentation time stamp, but will contain no actual video data.

您应该从 didOutputSampleBuffer: 委托回调中执行此操作。