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
How to Initialise Cvpixelbufferref in Swift
Swift: Deallocate Gamescene After Transition to New Scene
Cannot Assign to Property: 'Self' Is Immutable, I Know How to Fix But Needs Understanding
Using a Property as a Default Parameter Value for a Method in the Same Class
Create Instance of Class Known at Runtime in Swift
Sliding One Swiftui View Out from Underneath Another
Centering the Camera on a Node in Swift Spritekit
Why Don't Structs Have Deinitializers in Swift Like Classes
Stopping an Running Skaction - Sprite Kit
Add Links to Swift Classes in the Quick Help Documentation Comments
Swift: How to Open File with Associated Application
An Nsmanagedobject of Class 'Classname' Must Have a Valid Nsentitydescription.' Error
Nsbutton with Round Corners and Background Color
How to Pass Structure by Reference