Re: Rotating an NSImage around it's own center as well as offset to origin
Re: Rotating an NSImage around it's own center as well as offset to origin
- Subject: Re: Rotating an NSImage around it's own center as well as offset to origin
- From: Ron Fleckner <email@hidden>
- Date: Mon, 22 Feb 2010 11:22:08 +1100
On 22/02/2010, at 10:43 AM, Alexander Hartner wrote:
I would like to rotate and offset an NSImage and then draw it onto
an NSImageView. So far I figured out to use Quartz and use
CGContextRotateCTM and CGContextTranslateCTM. I got the offset
working just fine using this, but as soon as I introduce the
rotation it rotates the image relative to the origin, rather then
the centre. I figure I need to do a combination of
CGContextTranslateCTM to move the centre of the image to the origin,
then apply a CGContextRotateCTM to rotate it and then move it back,
but I can't figure out how to best do this.
Are there any other options of doing this please let me know. I had
a look online, but all the suggestions I found seem too complicated
as there must be a simple solution to this (I hope).
CGContextSaveGState(context);
CGContextRotateCTM(context, foregrounRotation);
CGContextTranslateCTM (context, foregroundXoffset,
foregroundYoffset);
CGContextDrawImage(context, imageRect, imageBackground);
CGImageRelease(imageBackground);
CGContextRestoreGState(context);
...
Thanks in advance
Alex
To rotate around the centre of an image, first translate (move) the
image origin to the centre of your frame, then rotate, then move the
image back. You've rotated first and then moved it.
CGContextTranslateCTM(ctx, distanceXtoCentre, distanceYtoCentre);
CGContextRotateCTM(ctx, rotationValue);
CGContextTranslateCTM(ctx, -distanceXtoCentre, -distanceYtoCentre);
HTH,
Ron
_______________________________________________
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