Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - UIImageView - how to handle UIImage image orientation

Is it possible to setup UIImageView to handle image orientation? When I set the UIImageView to image with orientation RIGHT (it is photo from camera roll), the image is rotated to right, but I want to show it in proper orientation, as it was taken.

I know I can rotate image data but it is possible to do it more elegant?

like image 218
Martin Pilch Avatar asked Jan 18 '12 18:01

Martin Pilch


People also ask

What is the difference between UIImage and UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .

How do I change the orientation of an image in CSS?

Once the CSS code is applied to your . css file, stylesheet, or <style> tags, you can use the CSS class name in any of your image tags. To rotate an image by another measure of degrees, change the "180" in the CSS code and <img> tag to the degree you desire.

How do I change the orientation of a picture in Swift?

You can get the orientation by using image. imageOrientation .

What is EXIF orientation?

The EXIF orientation value is used by Photoshop and other photo editing software to automatically rotate photos, saving you a manual task.


2 Answers

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.

like image 66
Tommy Avatar answered Sep 28 '22 01:09

Tommy


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.

like image 37
user2067021 Avatar answered Sep 28 '22 02:09

user2067021