CMSampleBufferGetImageBuffer(sampleBuffer)返回nil

时间:2018-07-03 08:46:51

标签: ios swift avfoundation core-media

我使用此代码从摄像机捕获视频,但是CMSampleBufferGetImageBuffer(sampleBuffer)始终返回nil。问题是什么?。这是代码,我修改了此源代码以适应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)
}

1 个答案:

答案 0 :(得分:5)

您正试图从“刚放下一个示例缓冲区”回调中访问像素缓冲区。头文件说:

  

传递给此委托方法的CMSampleBuffer对象将包含有关丢弃的视频帧的元数据,例如其持续时间和演示时间戳记,但将不包含实际的视频数据。

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