Orientation does not behave correctly with Photo in ALAsset

LittleFunny picture LittleFunny · Jan 12, 2012 · Viewed 8.9k times · Source

I current have an app that uses ALAsssetsLibrary to fetch the photos. I have placed the photo to an image view and I am able to upload to the server. When I tested on the real device after taking some photos, I found out the photos that supposed to be taken in Portrait become a landscape.

Therefore, I called different function to get the CGImage like this:

UIImage *image = [UIImage imageWithCGImage:[representation fullResolutionImage] scale:1.0 orientation:(UIImageOrientation)[representation orientation]];

The first tried out, I used this :

UIImage *image = [UIImage imageWithCGImage:[representation fullResolutionImage]]

I thought the one with scale and orientation could give me the right orientation that the photo was taken. But it didn't give me the right solution.

Do I miss anything that is necessary to generate a correct orientation of photo?

Answer

holtmann picture holtmann · Feb 25, 2012

The correct orientation handling depends on the iOS version you are using. On iOS4 and iOS 5 the thumbnail is already correctly rotated, so you can initialize your UIImage without specifying any rotation parameters. However for the fullScreenImage, the behavior is different for each iOS version. On iOS 5 the image is already rotated on iOS 4 not.

So on iOS4 you should use:

ALAssetRepresentation *defaultRep = [asset defaultRepresentation];
UIImage *_image = [UIImage imageWithCGImage:[defaultRep fullScreenImage] 
                                                     scale:[defaultRep scale] orientation:(UIImageOrientation)[defaultRep orientation]];

On iOS5 the following code should work correctly:

ALAssetRepresentation *defaultRep = [asset defaultRepresentation];
UIImage *_image = [UIImage imageWithCGImage:[defaultRep fullScreenImage] scale:[defaultRep scale] orientation:0];

Cheers,

Hendrik