Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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....

like image 997
Lee Armstrong Avatar asked May 27 '09 19:05

Lee Armstrong


People also ask

How do I rotate UIImageView?

Step 1 − Open Xcode→SingleViewApplication→name it RotateImage. Step 2 − Open Main. storyboard, add UIImageView and add 2 buttons as shown below name them ROTATE BY 90 DEGREES AND ROTATE BY 45 DEGREES.

How do I rotate an image in Swift?

Go to the ViewController. swift file and implement the rotateImage method. The image will rotate 180 degrees using the CGAffineTranform function. Build and Run the project and tap the Rotate Button to rotate the image.


1 Answers

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.

like image 110
Andrew Pouliot Avatar answered Nov 03 '22 01:11

Andrew Pouliot