如何创建样本缓冲区(CMSampleBufferRef)的实例?

And*_*lin 1 camera objective-c ios cmsamplebufferref

我尝试编写ios相机,然后从apple中获取了部分代码:

- (void)captureOutput:(AVCaptureOutput *)captureOutput
         didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
         fromConnection:(AVCaptureConnection *)connection
{
    // Create a UIImage from the sample buffer data
    UIImage *image = [self imageFromSampleBuffer:sampleBuffer];

     < Add your code here that uses the image >

}
Run Code Online (Sandbox Code Playgroud)

我需要在程序的任何位置调用此函数。但是因为它需要创建一个对象类型(CMSampleBufferRef)。怎么做?

我试图写类似:

buf1 = [[CMSampleBufferRef alloc]init] 
Run Code Online (Sandbox Code Playgroud)

但这是错误的方式。

Rot*_*mir 6

这是我目前CMSampleBuffer在swift3中用于单元测试模拟的一个片段:

fileprivate func getCMSampleBuffer() -> CMSampleBuffer {
    var pixelBuffer : CVPixelBuffer? = nil
    CVPixelBufferCreate(kCFAllocatorDefault, 100, 100, kCVPixelFormatType_32BGRA, nil, &pixelBuffer)

    var info = CMSampleTimingInfo()
    info.presentationTimeStamp = kCMTimeZero
    info.duration = kCMTimeInvalid
    info.decodeTimeStamp = kCMTimeInvalid


    var formatDesc: CMFormatDescription? = nil
    CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault, pixelBuffer!, &formatDesc)

    var sampleBuffer: CMSampleBuffer? = nil

    CMSampleBufferCreateReadyWithImageBuffer(kCFAllocatorDefault,
                                             pixelBuffer!,
                                             formatDesc!,
                                             &info,
                                             &sampleBuffer);

    return sampleBuffer!
}
Run Code Online (Sandbox Code Playgroud)