views:

960

answers:

1

I need to get CGContextRef of NSView object. It won't be so bad if I knew how.

In Carbon this thing was done like this:

CGContextRef cgref = (libvlc_drawable_t)GetWindowPort(HIViewGetWindow((OpaqueControlRef*)hiViewRef));

Obviously it can be done by subclassing NSView (or it's subclass) and catching it in it's drawRect, but that's too ugly.

Your ideas?

+2  A: 

I've never used it but it think this is your way:

[myView lockFocus];
imageContext = (CGContextRef)[[NSGraphicsContext currentContext]
                                     graphicsPort];
... 
[myView unlockFocus];

also have a look to the lockFocusIfCanDraw of NSView

IlDan
That's what I'd do, too, but it's worth clarifying that you probably shouldn't count on that context being valid past the `unlockFocus` message.
Peter Hosey
Thought of it too, but what's interesting, that on MS Windows, I use window id, which is also supposed to be impermanent, and it works fine (see my comment in the question block)
Maleev