NSAffineTransform Examples

dancedrummer
Unregistered
 
Post: #1
Greetings. I'm a newbie to Cocoa, bigitme. What I want to do is basic, rotate an image in an NSView. Can't find any examples or info that's simple enough for me to get a good grasp on this. I believe this must be done with NSAffineTransform. The closest I can get on my own is applying the transform and getting the image rotated but I'm only getting the upper right hand corner of the image displayed/rotated.

Any help would be greatly appreciated. Prefer a very simple bare bones example.

Thanks!
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
caution, code written in Safari and not tested.

Code:
NSAffineTransform *transform = [NSAffineTransform transform];
[transform translateXBy:0.5f * [image width] yBy:0.5f * [image height]];
[transform rotateByDegrees:45.0f];
[transform translateXBy:-0.5f * [image width] yBy:-0.5f * [image height]];
[NSGraphicsContext saveGraphicsState];
[transform concat];
[image compositeToPoint:NSMakePoint(0.0f, 0.0f) operation:NSCompositeSourceOver];
[NSGraphicsContext restoreGraphicsState];
Quote this message in a reply
dancedrummer
Unregistered
 
Post: #3
many thanks and way to code from the hip! save/restoreGraphicState is what i was missing. i couldn't get your example to work with [compositeToPoint, operation] but i did get it to work with [drawAtPoint, fromRect, operation, fraction]. thanks again, this works very nicely. my small change is below for anyone else interested.

NSAffineTransform *transform = [NSAffineTransform transform];
[transform translateXBy:0.5f * [image width] yBy:0.5f * [image height]];
[transform rotateByDegrees:45.0f];
[transform translateXBy:-0.5f * [image width] yBy:-0.5f * [image height]];
[NSGraphicsContext saveGraphicsState];
[transform concat];
//[image compositeToPoint:NSMakePoint(0.0f, 0.0f) operation:NSCompositeSourceOver];
[image drawAtPoint:NSMakePoint(0.0f, 0.0f)
fromRect:[self bounds]
operation:NSCompositeSourceOver
fraction:1];
[NSGraphicsContext restoreGraphicsState];
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Porting examples from an introductory 3D book crazy_ut2k4 4 3,202 Feb 16, 2007 08:04 AM
Last Post: AnotherJake
  Physics Examples/Help With Differentials Nick 1 2,183 Jan 14, 2006 05:29 PM
Last Post: Josh
  NSTimer/NSAffineTransform Coin 6 4,004 Feb 4, 2005 06:23 PM
Last Post: Coin