我使用此代码从相机捕获视频,但 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)
}

最佳答案

您正在尝试从“刚刚删除示例缓冲区”回调中访问像素缓冲区。头文件说:



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

关于ios - CMSampleBufferGetImageBuffer(sampleBuffer) 返回 nil,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51150267/

10-13 03:55