Skip to content

Instantly share code, notes, and snippets.

@alejandro-isaza
Last active June 22, 2017 09:51
Show Gist options
  • Save alejandro-isaza/5717438 to your computer and use it in GitHub Desktop.
Save alejandro-isaza/5717438 to your computer and use it in GitHub Desktop.
UIImage category to normalize image orientation by rotating an image so that it's orientation is UIImageOrientationUp. Based on http://stackoverflow.com/questions/5427656/ios-uiimagepickercontroller-result-image-orientation-after-upload/5427890#10611036
@interface UIImage (Orientation)
- (UIImage*)imageByNormalizingOrientation;
@end
@implementation UIImage (Orientation)
- (UIImage*)imageByNormalizingOrientation {
if (self.imageOrientation == UIImageOrientationUp)
return self;
CGSize size = self.size;
UIGraphicsBeginImageContextWithOptions(size, NO, self.scale);
[self drawInRect:(CGRect){{0, 0}, size}];
UIImage* normalizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return normalizedImage;
}
@end
@NikhilManapure
Copy link

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