【问题标题】:How to create CVPixelBuffer attributes dictionary in Swift如何在 Swift 中创建 CVPixelBuffer 属性字典
【发布时间】:2016-03-08 17:05:20
【问题描述】:

要在 Objective-C 中创建 CVPixelBuffer 属性,我会这样做:

NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                         [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
                         [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
                         nil];

然后在CVPixelBufferCreate 肉类中,我将(__bridge CFDictionaryRef) attributes 作为参数传递。

Swift 中,我正在尝试像这样创建我的字典:

let attributes:[CFString : NSNumber] = [
        kCVPixelBufferCGImageCompatibilityKey : NSNumber(bool: true),
        kCVPixelBufferCGBitmapContextCompatibilityKey : NSNumber(bool: true)
    ]

但我发现 CFString 不是可散列的,而且我一直无法让它工作。

谁能提供一个例子来说明这在 Swift 中是如何工作的?

【问题讨论】:

    标签: swift cvpixelbuffer cfdictionary


    【解决方案1】:

    只需使用 NSString 代替:

    let attributes:[NSString : NSNumber] = // ... the rest is the same
    

    毕竟,这正是您在 Objective-C 代码中所做的事情;只是 Objective-C 为你做了桥接。 CFString 不能是 Objective-C 字典中的键,也不能是 Swift 字典中的键。

    另一种(也许是 Swiftier)方式是这样写:

    let attributes : [NSObject:AnyObject] = [
        kCVPixelBufferCGImageCompatibilityKey : true,
        kCVPixelBufferCGBitmapContextCompatibilityKey : true
    ]
    

    请注意,通过这样做,我们也不必将true 包装在 NSNumber 中;这将由 Swift 的自动桥接为我们处理。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-14
      • 2016-02-18
      • 2023-04-03
      • 1970-01-01
      • 2019-01-03
      • 2023-03-21
      相关资源
      最近更新 更多