How to Convert Copaquepointer in Swift to Some Type (Cgcontext? in Particular)

Convert CFContextRef to mutable void pointer in swift?

I ended up with the reinterpretCast function returning intermediate Int variable for the bitmapContext address.

var bitmapContext: CGContext = CGBitmapContextCreate(...)

let bitmapContextAddress: Int = reinterpretCast(bitmapContext)
let bitmapContextPointer: CMutableVoidPointer = COpaquePointer(UnsafePointer<CGContext>(bitmapContextAddress))
let context = NSGraphicsContext(graphicsPort: bitmapContextPointer, flipped: false)

How to use Implicitly Unwrapped Optionals?

As of Swift 1.0

If your deployment target is 10.10 you can use the convenience method introduced with Yosemite.

let context = NSGraphicsContext.currentContext().CGContext

If you have to support 10.9 you'll have to cast the context manually as per below.

let contextPtr = NSGraphicsContext.currentContext().graphicsPort
let context = unsafeBitCast(contextPtr, CGContext.self)


Related Topics



Leave a reply



Submit