6

这是我在以前版本的 Swift 中的可行代码:

 let imageOptionsDictKeys = [ kCVPixelBufferPixelFormatTypeKey, kCVPixelBufferWidthKey,  kCVPixelBufferHeightKey, kCVPixelBufferOpenGLESCompatibilityKey, kCVPixelBufferIOSurfacePropertiesKey]
 let imageOptionsDictValues = [ cvPixelFormatType,  frameW, frameH, boolYES]

 var keyCallbacks = kCFTypeDictionaryKeyCallBacks
 var valueCallbacks = kCFTypeDictionaryValueCallBacks

 let imageOptions = CFDictionaryCreate(kCFAllocatorDefault, UnsafeMutablePointer(imageOptionsDictKeys), UnsafeMutablePointer(imageOptionsDictValues), 4, &keyCallbacks, &valueCallbacks)

在 Swift 3.0 中更改后,我必须将键和值数组转换UnsafeMutablePointer<UnsafeRawPointer?>为创建 CFDictionary。

这边走:

    let imageOptionsDictKeysPointer =  UnsafeMutablePointer<UnsafeRawPointer?>.allocate(capacity: 1)
    imageOptionsDictKeysPointer.initialize(to: imageOptionsDictKeys)

给出错误访问错误。

在阅读文档后,我正在尝试编译此代码:

        let imageOptionsDictKeys = [kCVPixelBufferPixelFormatTypeKey, kCVPixelBufferWidthKey,  kCVPixelBufferHeightKey, kCVPixelBufferOpenGLESCompatibilityKey]
        let imageOptionsDictKeysRawPointer = Unmanaged.passUnretained(imageOptionsDictKeys).toOpaque()
        let imageOptionsDictKeysPointer =  UnsafeMutablePointer<UnsafeRawPointer?>.allocate(capacity: 1)
        imageOptionsDictKeysPointer.initialize(to: imageOptionsDictKeysRawPointer)

        let imageOptionsDictValues = [ cvPixelFormatType,  frameW, frameH, boolYES]
        let imageOptionsDictValuesRawPointer = Unmanaged.passUnretained(imageOptionsDictValues).toOpaque()
        let imageOptionsDictValuesPointer =  UnsafeMutablePointer<UnsafeRawPointer?>.allocate(capacity: 1)
        imageOptionsDictValuesPointer.initialize(to: imageOptionsDictValuesRawPointer)

        let imageOptions = CFDictionaryCreate(kCFAllocatorDefault, imageOptionsDictKeysPointer, imageOptionsDictValuesPointer, 4, &keyCallbacks, &valueCallbacks)

但无法推断出错误 通用参数“实例”出现在Unmanaged.passUnretained( array ).toOpaque()行中

我现在不知道如何以编程方式创建 CFDictionary。

4

1 回答 1

-1

UnsafeMutablePointer< UnsafeMutablePointer<T>>我刚刚解决了将数组转换为您可以在此处找到 的类似问题: Swift 3 UnsafeMutablePointer initialization for C type float**

要使用相同的方案转换 swift 数组,请UnsafeMuTablePointer按照此处的建议使用:http: //technology.meronapps.com/2016/09/27/swift-3-0-unsafe-world-2/

于 2016-10-19T15:22:53.453 回答