我是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
答案 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()
}