• 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
Flipping an NSGraphicsContext [Was: Drawing with layout manager in a non-flipped view]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Flipping an NSGraphicsContext [Was: Drawing with layout manager in a non-flipped view]


  • Subject: Flipping an NSGraphicsContext [Was: Drawing with layout manager in a non-flipped view]
  • From: Antonio Nunes <email@hidden>
  • Date: Sun, 4 Mar 2007 15:07:48 +0000

Hi,

This is a followup on my previous message with a solution to a problem I ran into when testing my solution to text drawing with a layout manager in a non-flipped view: In my app matters are further complicated because under some circumstances the drawing code is not targeting the dynamically flippable NSView subclass, but a CGContext instead. That context is not flipped and, just like the NSView, I temporarily need to flip it. So how to do that? Well, it turns out not to be too hard: change the drawing context to a flipped clone of the current context. This will preserve the existing graphics state, including any transformations that may already have been applied. The only difference is that the coordinates are flipped. The following snippet shows how to do it.


if (<NOT DRAWING TO CGCONTEXT>) {
[view setFlipped:YES]; // This will make the text be drawn upside down
} else {
// We are drawing to a CGContext that is not flipped, and we can't dynamically flip it,
// so we create a flipped clone of the context to draw in to.
originalContext = [NSGraphicsContext currentContext];
NSGraphicsContext *nsgc = [NSGraphicsContext graphicsContextWithGraphicsPort:[originalContext graphicsPort] flipped:YES];
[NSGraphicsContext setCurrentContext:nsgc];
}


	// Drawing code here

	if (<NOT DRAWING TO CGCONTEXT>) {
		[view setFlipped:NO];
	} else {
		[NSGraphicsContext setCurrentContext: originalContext];
	}

So we test whether we are drawing to a CGContext, and if we are we save the current graphics state, obtain a new graphicsContext to the graphicsPort of the currentContext, but with flipped coordinates and set the current context to that. Then we draw as needed, and finally restore the non-flipped context after we are done drawing. The same drawing precautions apply as to drawing to the temporarily flipped NSView (see OP for that).

Cheers,
António

----------------------------------------------------
Energy is like a muscle,
it grows stronger through being used.
----------------------------------------------------


_______________________________________________

Cocoa-dev mailing list (email@hidden)

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


  • Prev by Date: Re: NSArray instance variable and core data objects
  • Next by Date: Re: NSUserDefaults won't do as it's told
  • Previous by thread: Re: QuartzCore.h not being recognized
  • Next by thread: i-bar not blinking
  • Index(es):
    • Date
    • Thread