iOS - UIImageView - how to handle UIImage image orientation iOS - UIImageView - how to handle UIImage image orientation ios ios

iOS - UIImageView - how to handle UIImage image orientation


If I understand, what you want to do is disregard the orientation of the UIImage? If so then you could do this:

UIImage *originalImage = [... whatever ...];UIImage *imageToDisplay =     [UIImage imageWithCGImage:[originalImage CGImage]              scale:[originalImage scale]              orientation: UIImageOrientationUp];

So you're creating a new UIImage with the same pixel data as the original (referenced via its CGImage property) but you're specifying an orientation that doesn't rotate the data.


You can completely avoid manually doing the transforms and scaling yourself, as suggested by an0 in this answer here:

- (UIImage *)normalizedImage {    if (self.imageOrientation == UIImageOrientationUp) return self;     UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);    [self drawInRect:(CGRect){0, 0, self.size}];    UIImage *normalizedImage = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();    return normalizedImage;}

The documentation for the UIImage methods size and drawInRect explicitly states that they take into account orientation.


Swift 3.1

func fixImageOrientation(_ image: UIImage)->UIImage {    UIGraphicsBeginImageContext(image.size)    image.draw(at: .zero)    let newImage = UIGraphicsGetImageFromCurrentImageContext()    UIGraphicsEndImageContext()    return newImage ?? image}