Rotating an image
Rotating an image
- Subject: Rotating an image
- From: Development <email@hidden>
- Date: Thu, 4 Oct 2007 21:36:29 -0700
Ok, I can rotate the view of an image easily by setting the rotation
of the NSImageView. but I need to be able to actually rotate the
image in the view and then adjust the size of the view and redisplay.
I thought that this would be done by NSBezierPath and
NSAffineTransform. I still think this is the case but my code only
produces either completely blank images OR completely black images or
an image with a black rectangle offset to the right by some number of
pixels or another. I've spent the past two hours on the developer
site and in google but the answer has not been apparent to me. Below
is the flawed code. I have subclassed NSImage view and added this
code to the subclass:
-(NSImage*)rotateImage:(float)degrees
{
NSRect sourceImageRect = [[self cell] rectCoveredByImageInBounds:
[self bounds]];
NSAffineTransform * xform = [NSAffineTransform transform];
NSBezierPath * xformedPath = [NSBezierPath
bezierPathWithRect:sourceImageRect];
[xform rotateByDegrees:degrees];
xformedPath = [xform transformBezierPath:[self imagePath]];
NSRect newRect =[xformedPath bounds];
NSImage * newImage = [[NSImage alloc]initWithSize:newRect.size];
[newImage lockFocus];
[[NSColor blackColor] set];
[xformedPath fill];
[[self image] drawInRect:newRect fromRect:sourceImageRect
operation:NSCompositeSourceIn fraction:1.0];
[newImage unlockFocus];
return newImage;
}
- (NSBezierPath *) imagePath {
NSRect sourceImageRect = [[self cell] rectCoveredByImageInBounds:
[self bounds]];
return [NSBezierPath bezierPathWithRect:sourceImageRect];
}
_______________________________________________
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