[英]How do I convert this to Swift?
attachments is a CFDictionaryRef. 附件是CFDictionaryRef。 How do I accomplish the (_bridge NSDictionary *) functionality in Swift?
如何在Swift中完成(_bridge NSDictionary *)功能?
CIImage *ciImage = [[CIImage alloc] initWithCVPixelBuffer:pixelBuffer
options:(__bridge NSDictionary *)attachments];
UPDATE UPDATE
here is the full code section I have tried for creating the CIImage. 这是我尝试创建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's answer is correct, but because attachments is an 'unmanaged' CFDictionary you have to take the unretainedValue of the dictionary in order to clear the error. @NobodyNada的答案是正确的,但是由于附件是“不受管理的” CFDictionary,因此您必须采用字典的unretainedValue才能清除错误。 The correct answer is:
正确的答案是:
var ciImage:CIImage = CIImage(CVPixelBuffer: pixelBuffer, options: attachments.takeUnretainedValue())
That is called toll-free bridging, and it allows you to convert between certain Foundation and CoreFoundation types with a simple cast. 这称为免费桥接,它允许您通过简单的转换在某些Foundation和CoreFoundation类型之间进行转换。 The
__bridge
thing was added with ARC because without it, ARC couldn't figure out enough information about it. __bridge
是在ARC中添加的,因为没有它,ARC无法找出有关它的足够信息。 NSDictionaries and CFDictionaries are interchangeable in Swift without a cast: NSDictionaries和CFDictionaries在Swift中可以互换,而无需强制转换:
let ciImage = CIImage(buffer: pixelBuffer, options: attachments).takeUnretainedValue()
PS Hi again:) Sorry I couldn't answer your other question; 再次提醒您:)对不起,我无法回答您的其他问题; I had to go suddenly.
我不得不突然去。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.