ios - 我如何将其转换为 Swift?

标签 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)

}

回答

@NobodyNada 的答案是正确的,但由于附件是“非托管”CFDictionary,因此您必须采用字典的 unretainedValue 才能清除错误。正确答案是:

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

最佳答案

这称为免费桥接,它允许您通过简单的转换在某些 Foundation 和 CoreFoundation 类型之间进行转换。 __bridge 这个东西是用 ARC 添加的,因为没有它,ARC 就无法找出足够的信息。 NSDictionaries 和 CFDictionaries 在 Swift 中可以互换,无需强制转换:

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

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

关于ios - 我如何将其转换为 Swift?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29357242/

相关文章:

ios - Xib 导出 View swift 变为 nil

ios - 如何获得准确的电池电量?

ios - NSDateFormatter 获取格式为 'MMM dd, yyyy HH:mm a' 的日期 nil

ios - titanium mobile navgroup.open 新窗口完全呈现之前的过渡

ios - 如何创建一个圆形的 UIImageView

ios - 如何通过 iMessage ios 8 发送音频文件

ios - 通过搜索选择多个单元格

objective-c - 是否有命令行界面可以列出Mac上范围内的所有蓝牙设备?

ios - 在 iOS swift 5 中获取 LAN 上的设备列表及其主机名和 IP 地址

swift - Swift 3 : Cannot convert value of type 'NSMutableDictionary' to expected argument type '[AnyHashable : Any]!'