【发布时间】:2017-02-19 07:40:39
【问题描述】:
这是我在以前版本的 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)
但错误Generic parameter 'Instance' could not be inferred出现在Unmanaged.passUnretained(array).toOpaque()
我现在不知道如何以编程方式创建 CFDictionary。
【问题讨论】:
-
不要创建 CFDictionary。而是创建一个 Swift 字典,例如这里:stackoverflow.com/a/38171414/1187415.
-
我必须使用 CFDictionary 参数调用函数 VTCompressionSessionCreate(....)。我不确定我是否可以只使用 Swift 字典......或者它们是否可以以某种方式转换?
-
似乎是一种不必要的复杂方法来制作
CFDictionary和教科书XY Problem。见stackoverflow.com/q/29301302/3141234 -
现在我只是简化了 CFDictionary 的创建。看起来字典已创建,但我的下一步仍然无法正常工作。可能是因为这本词典,我不确定stackoverflow.com/questions/39987373/…
标签: ios swift swift3 unsafe-pointers unsafemutablepointer