I'm just trying to rotate a UIImage around its center using the following piece of code:
- (UIImage *) rotateImage: (UIImage *)image angle:(CGFloat)angleInRadian
{
float newSide = MAX([image size].width, [image size].height);
CGSize size = CGSizeMake(newSide, newSide);
UIGraphicsBeginImageContext(size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, newSide/2, newSide/2);
CGContextRotateCTM(ctx, angleInRadian);
CGContextDrawImage(UIGraphicsGetCurrentContext(),CGRectMake(-[image size].width/2,-[image size].height/2,size.width, size.height),image.CGImage);
//CGContextTranslateCTM(ctx, [image size].width/2, [image size].height/2);
UIImage *i = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return i;
}
Here is a sample of the results I got:
Input image:
Result Image for angle = 0 radians :
The result image is flipped but I'm not sure why? Any idea about what I'm doing wrong?
Thanks in advance.
Add this line after CGContextRotateCTM
CGContextScaleCTM(ctx, 1.0, -1.0);
UIKit coordinate system is upside down to Quartz coordinate system. See here for more details
https://developer.apple.com/library/ios/qa/qa1708/_index.html
Also check image orientation.