On Jul 27, 2008, at 1:33 PM, [EMAIL PROTECTED] wrote:


-------------- Original message ----------------------
From: glenn andreas <[EMAIL PROTECTED]>
restoreGraphicsState restores the state of the current context - it
does not restore the context to the previous context. You need to
explicitly save the current context before you do setCurrentContext:

NSGraphicsContext *oldContext = [NSGraphicsContext currentContext];
[NSGraphicsContext setCurrentContext: nsContext];
... do drawing in the nsContext (save/restoreGraphics state impacts
internal variables of nsContext)
[NSGraphicsContext setCurrentContext: oldContext];


I think part of the difficulty I'm having is that I'm using CGGraphicsContext for the bulk of the drawing but found the CGxxx styled text drawing too tedious so switch to NSGraphic context methods just to draw the text and then attempt to switch back to CGGraphicsContext

You'll note that I'm passing in a CGContextRef to drawImageTextInCGContext and creating a temporary context just for the text drawing with this line:

NSGraphicsContext *nsContext = [NSGraphicsContext graphicsContextWithGraphicsPort: inContextRef flipped: NO];

Doesn't matter. When you do [NSGraphicsContext setCurrent: nsContext] you're changing global states associated with the current thread. You need to set it back to what it was before when you're done, or, at best, you'll get weird, inconsistent drawing (and more likely, crashes like you're seeing).



By my (perhaps flawed) understanding, I'm really just using the passed in context with an NSGraphics context wrapper, no? Do I restore the passed in context? And if so, how?


CG is "stateless", in that there is no "current context" (all drawing to a CGContext requires passing the CGContextRef). NSGraphicsContext, on the other hand, involves an implicit "current context". If you change that, you need to change it back - that's what currentContext/ setCurrentContext does. Just because NSGraphicsContext is built on top of CGContextRef doesn't mean that they have the same semantics (nor is NSGraphicsContext "just a wrapper" - it does a few more things).


Glenn Andreas                      [EMAIL PROTECTED]
 <http://www.gandreas.com/> wicked fun!
JSKit | the easy way to unite JavaScript and Objective C



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to