我如何将其转换为Swift?

时间:2015-03-30 22:09:26

标签: ios swift

附件是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)

}

ANSWER

@NobodyNada的答案是正确的,但因为附件是一个“不受管理的”而且#39; CFDictionary你必须取字典的unretainedValue才能清除错误。正确答案是:

var ciImage:CIImage = CIImage(CVPixelBuffer: pixelBuffer, options: attachments.takeUnretainedValue())

1 个答案:

答案 0 :(得分:4)

这称为免费桥接,它允许您使用简单的强制转换在某些Foundation和CoreFoundation类型之间进行转换。 __bridge事件被ARC添加,因为没有它,ARC无法找到足够的信息。 NSDictionaries和CFDictionaries在没有演员的情况下可以在Swift中互换:

let ciImage = CIImage(buffer: pixelBuffer, options: attachments).takeUnretainedValue()

P上。 S.嗨再次:)抱歉,我无法回答你的其他问题;我不得不突然去。