NSImage rotation regression?
NSImage rotation regression?
- Subject: NSImage rotation regression?
- From: Marco S Hyman <email@hidden>
- Date: Mon, 7 Sep 2009 16:21:36 -0700
This image rotation code works when compiled with the 10.5
SDK (64-bit, garbage collected).
- (NSImage *) rotateImage: (NSImage *) anImage
byDegrees: (CGFloat) degrees
{
// create an image for the rotated size
NSSize originalSize = [[anImage bestRepresentationForDevice:nil]
size];
NSSize rotatedSize;
if (degrees == 180.0)
rotatedSize = NSMakeSize(originalSize.width, originalSize.height);
else
rotatedSize = NSMakeSize(originalSize.height, originalSize.width);
NSImage *rotatedImage = [[NSImage alloc] initWithSize:
rotatedSize];
[rotatedImage lockFocus];
NSAffineTransform* transform = [NSAffineTransform transform];
NSPoint centerPoint = NSMakePoint(rotatedSize.width / 2,
rotatedSize.height / 2);
[transform translateXBy: centerPoint.x yBy: centerPoint.y];
[transform rotateByDegrees: degrees];
[transform translateXBy: -centerPoint.y yBy: -centerPoint.x];
[transform concat];
NSRect rect = NSMakeRect(0, 0, originalSize.width,
originalSize.height);
[[anImage bestRepresentationForDevice:nil] drawInRect: rect];
[rotatedImage unlockFocus];
return rotatedImage;
}
When the returned image is put in an NSImageView with -setImage: it
shows
up rotated and centered as expected. The same code does not work
when the SDK is changed to 10.6. The image is not rotated. If I move
-lockFocus to immediately before -drawInRect: the image is rotated, but
not scaled to fit in the NSImageView. Instead its origin is placed at
the lower left of the view with the upper portion of the image clipped.
Any pointers on where to look and what to read? I see that
-bestRepresentationForDevice: is deprecated and I've also read of some
underlying drawing changes, but don't understand how that may effect
my code.
Thanks,
/\/\arc
_______________________________________________
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