In Swift how do I convert CFStringRef[]
and CFTypeRef[]
to CMutablePointer< COpaquePointer >
?
I would like to call CFDictionaryCreate
from Swift. I have defined:
var keys: CFStringRef[] = [kCGImageSourceCreateThumbnailWithTransform, kCGImageSourceCreateThumbnailFromImageIfAbsent, kCGImageSourceThumbnailMaxPixelSize, kCGImageSourceCreateThumbnailFromImageAlways]
var values: CFTypeRef[] = [kCFBooleanTrue, kCFBooleanTrue, imageSize, kCFBooleanTrue]
CFDictionaryCreate(nil, keys, values, 4, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks)
but don't know how to specify keys and values in the above call.
I can get the compiler to stop complaining by creating the unsafe pointers for you, but I'm out of my depth on rest of the CFDictionary usage.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With