views:

948

answers:

2

I'm getting the UIImage from the image picker and putting it into a UIImageView. It works fine for regular images, but for images taken with the iphone camera, they sometimes come up rotated 90 degrees.

When accessing one of those images later (it has already been saved to disk) I noticed the above error.

Any ideas? I assume it has something to do with the iphone storing extra exif metadata about the camera position while the photo was taken, however one of the pictures was taken in portrait mode and somehow got rotated to landscape.

+1  A: 

The solution I've used is to rotate & flip the image as dictated by the imageOrientation property. There is some code here that will do the trick: UIImage fix.

A UIImageView rotates & flips automatically, so it's not a problem if all you do is display it on screen like that.

Chris Lundie
A: 

You are right that the phone stores rotation metadata with an image to indicate which way it should be rotated for display. This could be an EXIF tag; it could be something completely different, all Apple tells you is in the form of UIImage.

UIImage is in many respects, a simple wrapper around CGImage. CGImage image is in this case a "dumb" object that only knows/cares about raw image data; not anything so high-level as rotation info. UIImage brings this together with rotation and other info im a single convenient class. One of the perks is that UIImage automatically knows how to draw itself with the right rotation.

So the golden can basically be to avoid interacting with CGImage in most apps. UIImage should do almost everything you want, and handle higher level rotation info etc. too. Otherwise you get into odd situations where most images draw correctly, but some are rotated.

Mike Abdullah