Lee Armstrong
Lee Armstrong

Reputation: 11452

UIImage Rotation custom degrees

I have been using the code in this sample to assist and this works well. http://www.platinumball.net/blog/2009/03/30/iphone-uiimage-rotation-and-mirroring/

I cannot workout how to rotate by a custom amount of degrees between 0 & 360....

Upvotes: 8

Views: 15676

Answers (1)

Andrew Pouliot
Andrew Pouliot

Reputation: 5421

You'll want to do pretty much the same stuff as in that post does in rotate:

CGSize size = sizeOfImage;
UIGraphicsBeginImageContext(size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextRotateCTM(ctx, angleInRadians);
CGContextDrawImage(ctx, (CGRect){{}, size}, image);

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;

You might need to also translate the CTM in addition to rotating to compensate for the center of rotation. If you want to not crop the edges of the image when rotating, you should increase the size with some basic trig.

Upvotes: 14

Related Questions