8

I have the following code in swift which doesn't compile:

class CustomView: NSView {
  override func drawRect(dirtyRect: NSRect) {
    var contextPointer: COpaquePointer = NSGraphicsContext.currentContext()!.graphicsPort()
    var context: CGContext? = contextPointer as? CGContext
    CGContextSetRGBFillColor(context, 1, 0, 0, 1)
    CGContextFillRect(context, CGRectMake(0, 0, 100, 100))
    CGContextFlush(context)
  }
}

How do I convert COpaquePointer to CGContext?

5 Answers 5

10

As of Developer Preview 5, NSGraphicsContext now has a CGContext property. It's not documented yet but it's in the header file:

@property (readonly) CGContextRef CGContext NS_RETURNS_INNER_POINTER NS_AVAILABLE_MAC(10_10);
0
7

This is kind of ugly; it would be better if the graphicsPort() method was updated to return a CGContextRef directly (like UIGraphicsGetCurrentContext()), rather than a void*. I added this extension to NSGraphicsContext to sweep it under the rug for now:

extension NSGraphicsContext {
  var cgcontext: CGContext {
    return Unmanaged<CGContext>.fromOpaque(self.graphicsPort()).takeUnretainedValue()
  }
}

Just call NSGraphicsContext.currentContext().cgcontext anywhere you need a CGContext.

4
  • I wasn't aware of Unmanaged, or fromOpaque, but this works beautifully! Thanks!
    – alaroldai
    Jul 7, 2014 at 6:26
  • 1
    However, using the reinterpretCast sledgehammer seems to work
    – alaroldai
    Jul 8, 2014 at 7:44
  • What reinterpretCast? I typed that into Xcode doc search and got nothing. The new line code would help.
    – Rob N
    Jul 14, 2014 at 15:14
  • This works: return reinterpretCast(self.graphicsPort()); and here's an example gist Jul 16, 2014 at 2:24
3

This seems to be compiling:

var contextPointer = NSGraphicsContext.currentContext()!.graphicsPort()
let context = UnsafePointer<CGContext>(contextPointer).memory
3
  • Thanks! Not sure you need to use .currentContext()! though - isn't the type of currentContext already unwrapped? class func currentContext() -> NSGraphicsContext!
    – alaroldai
    Jun 3, 2014 at 9:18
  • @alaroldai Yes, but I need CGContext not NSGraphicsContext
    – Alfa07
    Jun 3, 2014 at 18:21
  • See stackoverflow.com/questions/24016668/…
    – klotz
    Sep 24, 2014 at 15:18
1

You are almost near. Try this code

class CustomView: NSView {
    override func drawRect(dirtyRect: NSRect) {

        var contextPointer = NSGraphicsContext.currentContext()!.graphicsPort()
        var context = UnsafePointer<CGContext>(contextPointer).memory

        CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0)
        CGContextFillRect(context, CGRectMake(0, 0, 100, 100))
        CGContextFlush(context)
    }
}
0

In Xcode 6.3 / Swift 1.2, NSGraphicsContext.currentContext()!.graphicsPort returns UnsafeMutablePointer<Void> and you can cast it into CGContextRef using unsafeBitCast because it's just a C pointer. This just reinterpret the pointer value, so should work exactly same with C forced point cast.

    let p1  =   NSGraphicsContext.currentContext()!.graphicsPort
    let ctx =   unsafeBitCast(p1, CGContextRef.self)

Please take care that such reinterpreting-cast is always very dangerous and used only when you're sure on what you're doing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.