如何在swift中将CMSampleBuffer转换为CMAttachmentBearer

时间:2015-01-03 14:38:05

标签: ios swift avfoundation

我是swift的新手,我想在CMCopyDictionaryOfAttachments委托中调用函数(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection

我的代码:

// MARK: Delegates

func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
    // got an image
    let pixelBuffer : CVPixelBufferRef = CMSampleBufferGetImageBuffer(sampleBuffer)
    let attachments : CFDictionaryRef = CMCopyDictionaryOfAttachments(kCFAllocatorDefault, sampleBuffer, CMAttachmentMode( kCMAttachmentMode_ShouldPropagate)) as CFDictionaryRef!

}

xcode导致错误:'CMSampleBuffer' is not identical to 'CMAttachmentBearer' 那么如何使用sampleBuffer作为目标,如果用objective-c

编写,这段代码就可以了

1 个答案:

答案 0 :(得分:1)

我想您代码中的主要问题是您使用CMSampleBuffer而不是CVPixelBufferRef

接下来的问题是CMCopyDictionaryOfAttachments返回一个非托管实例,需要使用takeRetainedValue()进行转换。

func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
    // got an image
    let pixelBuffer : CVPixelBufferRef = CMSampleBufferGetImageBuffer(sampleBuffer)
    let attachments : [NSObject : AnyObject] = CMCopyDictionaryOfAttachments(kCFAllocatorDefault, pixelBuffer, CMAttachmentMode( kCMAttachmentMode_ShouldPropagate)).takeRetainedValue()

}