Boeckm Boeckm - 7 days ago 5
Objective-C Question

iOS Image Orientation has Strange Behavior

For the past few weeks I've been working with images in objective-c and noticing a lot of strange behavior. First, like many other people, I've been having this problem where images taken with the camera (or taken with somebody else's camera and MMS'd to me) are rotated 90 degrees. I wasn't sure why in the world this was happening (hence my question) but I was able to come up with a cheap work around.

My question this time is why is this happening? Why is Apple rotating images? When I take a photo with my camera right-side up, unless I perform my code mentioned above, when I save the photo it gets saved rotated. Now, my workaround was okay up until a few days ago.

My application modifies individual pixels of an image, specifically the alpha channel of a PNG (so any JPEG conversion gets thrown out of the window for my scenario). A few days ago I noticed that even though the image is displayed properly in my app thanks to my workaround code, when my algorithm modifies the individual pixels of the image, it thinks the image is rotated. So instead of modifying the pixels at the top of the image, it modifies the pixels on the side of the image (because it thinks it should be rotated)! I can't figure out how to rotate the image in memory - ideally I would prefer to just wipe away that

imageOrientation
flag all together.

Here's something else that has been baffling me as well... When I take the photo, the
imageOrientation
is set to 3. My workaround code is smart enough to realize this and flip it so the user never notices. Additionally, my code to save the image to the library realizes this, flips it, then saves it so it appears in the camera roll properly.

That code looks like so:

NSData* pngdata = UIImagePNGRepresentation (self.workingImage); //PNG wrap
UIImage* img = [self rotateImageAppropriately:[UIImage imageWithData:pngdata]];
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);


When I load this newly saved image into my app, the
imageOrientation
is 0 - exactly what I want to see, and my rotation workaround doesn't even need to run (note: when loading images from the internet as opposed to images taken with a camera, the
imageOrientation
is always 0, resulting in perfect behavior). For some reason, my save code seems to wipe away this
imageOrientation
flag. I was hoping to just steal that code and use it to wipe away my imageOrientation as soon as the user takes a photo and has it added to the app, but it doesn't seem to work. Does
UIImageWriteToSavedPhotosAlbum
do something special with
imageOrientation
?

Would the best fix for this problem be to just blow away
imageOrientation
as soon as the user is done taking an image. I assume Apple has the rotation behavior done for a reason, right? A few people suggested that this is an Apple defect.

(... if you're not lost yet... Note2: When I take a horizontal photo, everything seems to work perfectly, just like photos taken from the internet)

EDIT:

Here are what some of the images and scenarios actually look like. Based off the comments so far, it looks like this strange behavior is more than just an iPhone behavior, which I think is good.

This is a picture of the photo I took with my phone (note the proper orientation), it appears exactly as it did on my phone when I snapped the photo:

Actual Photo taken on iPhone

Here is what the image looks like in Gmail after I emailed it to myself (looks like Gmail handles it properly):

Photo as it appears in Gmail

Here is what the image looks like as a thumbnail in windows (doesn't look like it is handled properly):

Windows Thumbnail

And here is what the actual image looks like when opened with Windows Photo Viewer (still not handled properly):

Windows Photo Viewer Version

After all of the comments on this question, here's what I'm thinking... The iPhone takes an image, and says "to display this properly, it needs to be rotated 90 degrees". This information would be in the EXIF data. (Why it needs to be rotated 90 degrees, rather than defaulting to straight vertical, I don't know). From here, Gmail is smart enough to read and analyze that EXIF data, and properly display it. Windows however, is not smart enough to read the EXIF data, and therefore displays the image improperly. Are my assumptions correct?

Answer

I did R&D on it and discovered , every image file has metadata property. If the metadata specifies the orientation of the image which is generally ignored by other OS but Mac. Most of images taken are having their meta data property set to right angle. So Mac shows it 90 degree rotated manner. You can see the same image in proper way in windows OS.

For more detail read this answer http://graphicssoft.about.com/od/digitalphotography/f/sideways-pictures.htm

try reading your image's exif here http://www.exifviewer.org/ , or http://regex.info/exif.cgi , or http://www.addictivetips.com/internet-tips/view-complete-exif-metadata-information-of-any-jpeg-image-online/