Re: Rotate NSImage to get a new NSImage, without drawing
Re: Rotate NSImage to get a new NSImage, without drawing
- Subject: Re: Rotate NSImage to get a new NSImage, without drawing
- From: Jerry Krinock <email@hidden>
- Date: Sat, 28 Feb 2009 22:20:44 -0800
On 2009 Feb 28, at 20:10, Graham Cox wrote:
Create the new image, swapping width and height, lock focus onto it,
apply a transform that rotates 90 degrees, and draw the first into
the second.
You can't do it without drawing, but the drawing doesn't need to be
onscreen.
Thanks, Graham. Fun stuff....
@interface NSImage (Transform)
/*!
@brief Rotates an image around its center by a given
angle in degrees and returns the new image.
@details The width and height of the returned image are,
respectively, the height and width of the receiver.
I have not yet tested this with a non-square image.
*/
- (NSImage*)imageRotatedByDegrees:(CGFloat)degrees ;
@end
#import "NSImage+Transform.h"
@implementation NSImage (Transform)
- (NSImage*)imageRotatedByDegrees:(CGFloat)degrees {
NSSize rotatedSize = NSMakeSize(self.size.height,
self.size.width) ;
NSImage* rotatedImage = [[NSImage alloc]
initWithSize:rotatedSize] ;
NSAffineTransform* transform = [NSAffineTransform transform] ;
// In order to avoid clipping the image, translate
// the coordinate system to its center
[transform translateXBy:+self.size.width/2
yBy:+self.size.height/2] ;
// then rotate
[transform rotateByDegrees:degrees] ;
// Then translate the origin system back to
// the bottom left
[transform translateXBy:-rotatedSize.width/2
yBy:-rotatedSize.height/2] ;
[rotatedImage lockFocus] ;
[transform concat] ;
[self drawAtPoint:NSMakePoint(0,0)
fromRect:NSZeroRect
operation:NSCompositeCopy
fraction:1.0] ;
[rotatedImage unlockFocus] ;
return [rotatedImage autorelease] ;
}
@end
_______________________________________________
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