7

The scenario:

I'm editing some CGImages and strangely enough until now I was first creating NSImages from those CGImages before drawing them. So I tried to change the code so that I would draw the CGImages directly in into NSGraphicsContext.currentContext().graphicsPort. As you see I'm using Swift here but the same problem goes for Objective-C I presume.

Having a look into the documentation I saw that graphicsPort will be deprecated in 10.10, but I couldn't find another way to the current CGContext from NSGraphicsContext nor is there a way to create the CGContext from an NSGraphicsContext.

The Question:

Does anybody know what the proper way to retrieve the context would be? If not - Is there a way to cast the graphicsPort's COpaquePointer, which apparently is an initialized NSPipeObject, to a CGContextRef? Although using code that has already been marked deprecated would be quite unsatisfactory I would consider it until there is a better solution.

1 Answer 1

9

After spending a half hour digging around for information on this, I realized I should just check out the header file for NSGraphicsContext. Turns out there's a new property on NSGraphicsContext that's apparently not documented yet: CGContext. The header also notes that this should be used instead of graphicsPort.

2
  • Well, after updating to the current beta version there was indeed this member in the header. But I guess there's a reason why it is not documented yet, since trying to use it throws an 'unrecognized selector' error. Using let contextPointer = NSGraphicsContext.currentContext()!.graphicsPort let context = UnsafePointer<CGContext>(contextPointer).memory would result in an 'invalid context'. So for now I'm back to using NSImages for drawing into my view again.
    – Enie
    Aug 8, 2014 at 8:25
  • Maybe I'm not understanding the comment, but isn't let context = NSGraphicsContext.currentContext()?.CGContext what you want?
    – sam
    Jul 6, 2015 at 6:09

Your Answer

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

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