Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Swift how do I convert CFStringRef[] and CFTypeRef[] to CMutablePointer< COpaquePointer >?

Tags:

swift

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.

like image 925
ilia Avatar asked Jun 12 '14 15:06

ilia


1 Answers

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)
like image 98
sketchyTech Avatar answered Sep 28 '22 11:09

sketchyTech