在Swift中如何将CFStringRef[]
和CFTypeRef[]
转换为CMutablePointer< COpaquePointer >
?
我想从Swift致电CFDictionaryCreate
。我已定义:
var keys: CFStringRef[] = [kCGImageSourceCreateThumbnailWithTransform, kCGImageSourceCreateThumbnailFromImageIfAbsent, kCGImageSourceThumbnailMaxPixelSize, kCGImageSourceCreateThumbnailFromImageAlways]
var values: CFTypeRef[] = [kCFBooleanTrue, kCFBooleanTrue, imageSize, kCFBooleanTrue]
CFDictionaryCreate(nil, keys, values, 4, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks)
但不知道如何在上述调用中指定键和值。
答案 0 :(得分:5)
我可以通过为你创建不安全的指针来让编译器停止抱怨,但是我对其余的CFDictionary用法不太了解。
import ImageIO
let dictionayKeyCB = UnsafePointer<CFDictionaryKeyCallBacks>()
let valueKeyCB = UnsafePointer<CFDictionaryValueCallBacks>()
let keys: [CFStringRef] = [kCGImageSourceCreateThumbnailWithTransform, kCGImageSourceCreateThumbnailFromImageIfAbsent, kCGImageSourceThumbnailMaxPixelSize, kCGImageSourceCreateThumbnailFromImageAlways]
let keysPointer = UnsafeMutablePointer<UnsafePointer<Void>>.alloc(1)
keysPointer.initialize(keys)
let values: [CFTypeRef] = [kCFBooleanTrue, kCFBooleanTrue, kCFBooleanTrue, kCFBooleanTrue]
let valuesPointer = UnsafeMutablePointer<UnsafePointer<Void>>.alloc(1)
valuesPointer.initialize(values)
CFDictionaryCreate(kCFAllocatorDefault, keysPointer, valuesPointer, 4, dictionayKeyCB, valueKeyCB)