4

I’m trying to get a correctly rotated UIImage from an ALAssetRepresentation using the fullScreenImage method. I have several testing photos shot in various device orientations; the photos show up correctly in the Photos app. The documentation for fullScreenImage says:

In iOS 5 and later, this method returns a fully cropped, rotated, and adjusted image—exactly as a user would see in Photos or in the image picker.

To create a correctly-rotated UIImage object from the CGImage, you use imageWithCGImage:scale:orientation: or initWithCGImage:scale:orientation:, passing the values of orientation and scale.

Given the docs, my code looks like this:

ALAssetRepresentation *rep = [asset defaultRepresentation];
UIImage *img = [UIImage
    imageWithCGImage:[rep fullScreenImage]
    scale:[rep scale]
    orientation:[rep orientation]];

But the resulting UIImage’s rotation is wrong. When I replace [rep orientation] with UIImageOrientationUp, the image is fine for all testing photos. Obviously I’m hesitating to stick with this “solution”, as it feels like a hack. What am I doing wrong?

zoul
  • 102,279
  • 44
  • 260
  • 354

1 Answers1

11
ALAssetRepresentation *rep = [asset defaultRepresentation];
UIImage *img = [UIImage
    imageWithCGImage:[rep fullScreenImage]
    scale:[rep scale]
    orientation:UIImageOrientationUp];

Is correct as under iOS 5 the fullscreenimages is already rotated (so it’s always “up”). Under iOS 4 the behaviour is different. Please see Orientation does not behave correctly with Photo in ALAsset for a more in depth explanation.

Community
  • 1
  • 1
holtmann
  • 6,043
  • 32
  • 44
  • Hello, Hendrik, and thanks again for the answer! I’ll submit a bug request against the documentation then, as it does not really make clear that the second paragraph only applies to iOS4. PS: you might want to leave out the signature from your posts, see [this meta thread](http://meta.stackexchange.com/questions/5029/are-taglines-signatures-disallowed). – zoul Jun 06 '12 at 08:14
  • Submitted as [rdar://11604456](http://openradar.appspot.com/11604456) and resolved a month later in the upcoming iOS 6 release by leaving out the second, slightly confusing paragraph. – zoul Jul 12 '12 at 06:55
  • Thank you! I was struggling with this (and other related problems) for close to a week now. This is the first answer that actually made sense and worked for iOS5/iOS6. You are my hero ;) – juhan_h Apr 09 '13 at 05:32
  • thank you very much. This solution is also valid for iOS7.0.6. – x4h1d Jun 28 '14 at 06:39
  • You rock! I was using `[UIImage imageWithCGImage:[rep fullResolutionImage]]` and your solution fixed the rotation problem, thank you! (Works on iOS 8.1) – Fouad Feb 26 '15 at 23:07