1

The size of A UIImage in my app is (320,460)

I created another UIImage object using

- (id)initWithCGImage:(CGImageRef)imageRef scale:(CGFloat)scale orientation:(UIImageOrientation)orientation

I assigned orientation to UIImageOrientationLeft.

Then I printed the new UIImage object's size, the result was (460,320).

It has rotated to left already.

I needed to store the UIImage in my document directory.

NSData *imageData = UIImagePNGRepresentation(rotateImageView);
NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

[imageData writeToFile:[path stringByAppendingPathComponent:@"test.png"] atomically:NO];      

But when I got the UIImage object from "test.png"

the size of it was changed to (320,460),it has rotated to its orignal status.

I wanted that it can be stored in (460,320)

Did I make some mistakes?

Thanks!

Solskjaer
  • 175
  • 1
  • 11

1 Answers1

1

I've run into this problem as well. When you pass around image orientations within Apple code, you don't actually rotate any pixel data. Rather, there is basically an enum value stored with the image. Many of Apple's image renderer's are smart enough to read this enum value, and use it to display the image properly. So the code snippets you share just change this enum value. The renderers that respect this value will display what you want, while many other renderers will ignore it.

There are a couple solutions available.

First, if you're displaying an image through iOS, you can use the transform property of UIImageView along with CGAffineTransformMakeRotation to get the desired orientation.

Second, you could actually rotate the raw pixel data, which can be accomplished like this:

How to rotate image file?

I would recommend the first solution, since it's easier to code, and more efficient. However, if you will be sharing these images outside of iOS, the second approach will give more reliable results.

Community
  • 1
  • 1
Tyler
  • 28,498
  • 11
  • 90
  • 106