15

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?

phi
  • 10,350
  • 6
  • 50
  • 82
LittleFunny
  • 7,205
  • 11
  • 71
  • 166

4 Answers4

45

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

holtmann
  • 5,957
  • 30
  • 42
  • In my case i needed the fullResolutionImage and have tested on iOS 6 and 7. The image was not automatically rotated but the first example code does the trick! Thank's man! P.S. be carefully if saving the image to file with metadata. If you have rotated the image then you must set the EXIF orientation to 0 to avoid strange rotated jpeg's ;) – Primoz990 Oct 25 '13 at 11:37
  • I must correct my comment. The first code example didn't rotate my fullResolution image, i had to rotate it by my self. But agree that the fullScreenImage is automatically rotated on iOS 6 and 7. – Primoz990 Oct 25 '13 at 12:15
  • Great ! It works for me too with iOS 7 ! Thanks a lot ! – Vinestro Feb 20 '14 at 17:16
4

Try this code:-

 UIImage* img = [UIImage imageWithCGImage:asset.thumbnail];
 img = [UIImage imageWithCGImage:img.CGImage scale:1.0 orientation:UIImageOrientationUp];

This may help you.

Leena
  • 2,652
  • 1
  • 28
  • 43
  • I can't try it now since i don't own the device. Do you mind explain to me those two lines of code? The img on the first line is the thumbnail of the asset but not the actual image. – LittleFunny Jan 12 '12 at 10:05
  • i have used thumbnail of the image and i have kept the orientation of image upwards so it won't be rotated it worked in my case. – Leena Jan 12 '12 at 10:20
  • Hmm I have a problem with UIImageOrientationUp for iPhone.... It works on iPad but not on iPhone. Does that mean UIImageOrientationUp is different on both device. – LittleFunny Jan 14 '12 at 03:47
  • it just makes orientation of your image upwards thats it. – Leena Jan 16 '12 at 05:15
3

My experience is limited to IOS 5.x but I can tell you that the thumbnail and fullscreen images are oriented properly. It's the fullresolutionimage that's horizontal when shot vertically. My solution is to use a category on uiimage that I got from here:

http://www.catamount.com/forums/viewtopic.php?f=21&t=967&start=0

It provides a nice rotating method on a UIImage like this:

        UIImage *tmp = [UIImage imageWithCGImage:startingFullResolutionImage];
        startingFullResolutionImage = [[tmp imageRotatedByDegrees:-90.0f] CGImage];
user953175
  • 205
  • 3
  • 9
0

For fullResolutionImage, I'd like to provide a solution as follows,

ALAssetRepresentation *rep = [asset defaultRepresentation];

// First, write orientation to UIImage, i.e., EXIF message.
UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage] scale:rep.scale orientation:(UIImageOrientation)rep.orientation];
// Second, fix orientation, and drop out EXIF
if (image.imageOrientation != UIImageOrientationUp) {
   UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
   [image drawInRect:(CGRect){0, 0, image.size}];
   UIImage *normalizedImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();
   image = normalizedImage;
}
// Third, compression
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);

imageData is what you want, and just upload it to your photo server.

By the way, if you think EXIF is useful, you can complement it to normalizedImage as you wish.

DawnSong
  • 3,232
  • 1
  • 26
  • 35