• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSGraphicsContext restore crashes my xtension
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSGraphicsContext restore crashes my xtension


  • Subject: Re: NSGraphicsContext restore crashes my xtension
  • From: glenn andreas <email@hidden>
  • Date: Sun, 27 Jul 2008 13:53:07 -0500


On Jul 27, 2008, at 1:33 PM, email@hidden wrote:


-------------- Original message ---------------------- From: glenn andreas <email@hidden>
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@hidden <http://www.gandreas.com/> wicked fun! JSKit | the easy way to unite JavaScript and Objective C



_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:
This email sent to email@hidden


References: 
 >Re: NSGraphicsContext restore crashes my xtension (From: email@hidden)

  • Prev by Date: Event-Driven XML Parsing and Entity References
  • Next by Date: Re: NSDocumentController, NSDocument and toolbars
  • Previous by thread: Re: NSGraphicsContext restore crashes my xtension
  • Next by thread: Re: NSGraphicsContext restore crashes my xtension
  • Index(es):
    • Date
    • Thread