Skip to content

Instantly share code, notes, and snippets.

@erans
Created November 7, 2011 07:10
Show Gist options
  • Save erans/1344380 to your computer and use it in GitHub Desktop.
Save erans/1344380 to your computer and use it in GitHub Desktop.
UIImage+Resize.m Fix for Rotation / Orientation on iOS 5
- (UIImage *)resizedImage:(CGSize)newSize interpolationQuality:(CGInterpolationQuality)quality {
BOOL drawTransposed;
CGAffineTransform transform = CGAffineTransformIdentity;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0) {
// Apprently in iOS 5 the image is already correctly rotated, so we don't need to rotate it manually
drawTransposed = NO;
} else {
switch (self.imageOrientation) {
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
drawTransposed = YES;
break;
default:
drawTransposed = NO;
}
transform = [self transformForOrientation:newSize];
}
return [self resizedImage:newSize
transform:transform
drawTransposed:drawTransposed
interpolationQuality:quality];
}
@erans
Copy link
Author

erans commented Nov 7, 2011

For better performance, make sure to store some global flag that you are running in iOS version >= 5.0 so that you would only check a boolean flag instead of casting a string to a float and checking its value.

@tonyarnold
Copy link

Have you switched out the imageWithCGImage: method calls for imageWithCGImage:scale:orientation:?

@erans
Copy link
Author

erans commented Feb 28, 2012

Thanks for your suggestion. The scale parameter wouldn't let me scale it to whatever I wanted (not just scaled). I'm quite sure that imageWithCGImage:scale:orientation: does a similar thing underneath.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment