• 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
Sample code to rotate an NSImage
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Sample code to rotate an NSImage


  • Subject: Sample code to rotate an NSImage
  • From: Dave Riggle <email@hidden>
  • Date: Thu, 18 Sep 2003 10:06:16 -0700

The following routine works, but I don't like it. The transformations seem to get applied in reverse order. Is that the way NSAffineTransform is supposed to work?

Dave

+ (NSImage *)rotate:(NSImage *)image byAngle:(int)degrees
{
if (degrees == 0) {
return image;
} else {
NSSize beforeSize = [image size];
NSSize afterSize = degrees == 90 || degrees == -90 ? NSMakeSize(beforeSize.height, beforeSize.width) : beforeSize;
NSImage* newImage = [[[NSImage alloc] initWithSize:afterSize] autorelease];
NSAffineTransform* trans = [NSAffineTransform transform];

[newImage lockFocus];
[trans translateXBy:afterSize.width * 0.5 yBy:afterSize.height * 0.5];
[trans rotateByDegrees:degrees];
[trans translateXBy:-beforeSize.width * 0.5 yBy:-beforeSize.height * 0.5];
[trans set];
[image drawAtPoint:NSZeroPoint
fromRect:NSMakeRect(0, 0, beforeSize.width, beforeSize.height)
operation:NSCompositeCopy
fraction:1.0];
[newImage unlockFocus];
return newImage;
}
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
  • Follow-Ups:
    • Re: Sample code to rotate an NSImage
      • From: Henry McGilton <email@hidden>
  • Prev by Date: Re: where's xcode?
  • Next by Date: Re: NSSound truncates the beginnings of sounds
  • Previous by thread: Re: where's xcode?
  • Next by thread: Re: Sample code to rotate an NSImage
  • Index(es):
    • Date
    • Thread