2015-01-03 60 views
0

我是新雨燕,我想调用函数CMCopyDictionaryOfAttachments(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection代表我的如何CMSampleBuffer转换为CMAttachmentBearer在迅速

代码:

// 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

我猜你的代码中的主要问题是,你通过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() 

}