如何在Swift中创建CVPixelBuffer属性字典

如何在Swift中创建CVPixelBuffer属性字典,swift,cvpixelbuffer,cfdictionary,Swift,Cvpixelbuffer,Cfdictionary,要在Objective-C中创建CVPixelBuffer属性,我将执行以下操作: NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey, [NSNumber numberW

要在Objective-C中创建CVPixelBuffer属性,我将执行以下操作:

NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                         [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
                         [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
                         nil];
然后在
CVPixelBufferCreate
方法中,我将
(uu-bridge-CFDictionaryRef)属性作为参数传递

Swift中,我试图创建如下词典:

let attributes:[CFString : NSNumber] = [
        kCVPixelBufferCGImageCompatibilityKey : NSNumber(bool: true),
        kCVPixelBufferCGBitmapContextCompatibilityKey : NSNumber(bool: true)
    ]
但我发现CFString是不可散列的,我一直无法让它工作


有人能提供一个在Swift中如何工作的例子吗?

只需使用NSString即可:

let attributes:[NSString : NSNumber] = // ... the rest is the same
毕竟,这就是你在Objective-C代码中真正做的事情;只是Objective-C在为你打桥牌。CFString不能像在Swift字典中一样成为Objective-C字典中的键

另一种(也许更快捷)的方式是这样写:

let attributes : [NSObject:AnyObject] = [
    kCVPixelBufferCGImageCompatibilityKey : true,
    kCVPixelBufferCGBitmapContextCompatibilityKey : true
]
注意,通过这样做,我们也不必将
true
包装在NSNumber中;这将由Swift的桥接自动为我们解决