我是 swift 的新手,我想在 (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection 中调用函数
委托(delegate)CMCopyDictionaryOfAttachments
我的代码:
// 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' 与 'CMattachmentBearer' 不同
那么我如何使用sampleBuffer作为目标,如果用objective-c编写此代码就可以工作
最佳答案
我想您代码中的主要问题是您传递了 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()
}
关于ios - 如何在 swift 中将 CMSampleBuffer 转换为 CMAttachmentBearer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27755958/