As Anomie suggests in their answer here, this code will help to fix orientation:
- (UIImage *)fixrotation:(UIImage *)image{
if (image.imageOrientation == UIImageOrientationUp) return image;
CGAffineTransform transform = CGAffineTransformIdentity;
switch (image.imageOrientation) {
case UIImageOrientationDown:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, image.size.width, image.size.height);
transform = CGAffineTransformRotate(transform, M_PI);
break;
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
transform = CGAffineTransformTranslate(transform, image.size.width, 0);
transform = CGAffineTransformRotate(transform, M_PI_2);
break;
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, 0, image.size.height);
transform = CGAffineTransformRotate(transform, -M_PI_2);
break;
case UIImageOrientationUp:
case UIImageOrientationUpMirrored:
break;
}
switch (image.imageOrientation) {
case UIImageOrientationUpMirrored:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, image.size.width, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
case UIImageOrientationLeftMirrored:
case UIImageOrientationRightMirrored
CGContext
withCGBitmapContextCreate
(or with theUIGraphicsBeginImageContext
shorthand), useCGContextRotateCTM
to set a rotation, use eitherdrawInRect:
on theUIImage
orCGContextDrawImage
with the image‘sCGImage
property, then convert the context to an image either withUIGraphicsGetImageFromCurrentImageContext
(and, then,UIGraphicsEndImageContext
) if you used UIKit to create the context, orCGBitmapContextCreateImage
if you were sticking with the Core Graphics. UIKit isn‘t very thread safe, but the code would be neater. – Tommy Mar 8 ‘12 at 20:31