附件是一个CFDictionaryRef。如何在Swift中完成(_bridge NSDictionary *)功能?
CIImage *ciImage = [[CIImage alloc] initWithCVPixelBuffer:pixelBuffer
options:(__bridge NSDictionary *)attachments];更新
下面是我尝试创建CIImage的完整代码部分。
func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
var pixelBuffer:CVPixelBufferRef = CMSampleBufferGetImageBuffer(sampleBuffer)
var attachmentMode = CMAttachmentMode(kCMAttachmentMode_ShouldPropagate)
var attachments = CMCopyDictionaryOfAttachments(kCFAllocatorDefault, sampleBuffer, attachmentMode)
var ciImage:CIImage = CIImage(CVPixelBuffer: pixelBuffer, options: attachments)
}答案
@NobodyNada的答案是正确的,但是由于附件是“非托管的”CFDictionary,所以必须使用字典的unretainedValue来清除错误。正确的答案是:
var ciImage:CIImage = CIImage(CVPixelBuffer: pixelBuffer, options: attachments.takeUnretainedValue())发布于 2015-03-30 22:23:15
这就是所谓的免费桥接,它允许您通过简单的转换在特定的基金会和CoreFoundation类型之间进行转换。__bridge是与ARC一起添加的,因为没有它,ARC就无法找到足够的信息。NSDictionaries和CFDictionaries在没有演员阵容的Swift中是可以互换的:
let ciImage = CIImage(buffer: pixelBuffer, options: attachments).takeUnretainedValue()对不起,我不能回答你的另一个问题,我不得不突然走了。
https://stackoverflow.com/questions/29357242
复制相似问题