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....
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With