3

Hi everyone,

I'm having an issue rotating a portrait UIImage (i.e. WIDTH < HEIGHT) that somehow has an orientation equal to UIImageOrientationRight. I'm not sure how this comes to be but it happens with a few images in my library captured with an iPhone 4.

This is my code (that does work but only if the orientation is equal to UIImageOrientationUp):

UIGraphicsBeginImageContextWithOptions(rotatedScaledRect.size, NO, 0.0);
CGContextRef myContext = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(myContext, kCGInterpolationHigh);

CGContextTranslateCTM(myContext,
                      (rotatedScaledRect.size.width/2),
                      (rotatedScaledRect.size.height/2));
CGContextConcatCTM(myContext, transformation);

CGContextScaleCTM(myContext, 1.0, -1.0);

CGContextDrawImage(myContext, CGRectMake(-roundf(newImage.size.width / 2), -roundf(newImage.size.height / 2), newImage.size.width, newImage.size.height), [newImage CGImage]);

newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

I need to apply CGContextConcatCTM because I have several concatenated transformations to the image.

I've tried several different approaches to no avail.

Thanks in advance for your help.

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
sunshineDev
  • 323
  • 3
  • 10
  • I have same issue, if the image orientation is not euqal to 0, this script doesnt work – Pion Apr 11 '13 at 21:46
  • There's a solution to this problem [here](http://stackoverflow.com/questions/20204495/mfmailcomposeviewcontroller-image-orientation) that also includes a lengthy explanation of what's actually going on. – Gallymon Nov 29 '13 at 04:33

1 Answers1

0

First of all, excuse me for my english, because it's not my native language. I hope it's not so late to give this answer!

I had a similar problem loading photos from library taken with iPhone or iPod cammera because of this. If that's your case, you can found some info here: https://discussions.apple.com/thread/2495567?start=30&tstart=0, specifically from an answer that says "Apple chose to use the Orientation flag in the EXIF data to inform the program displaying the image as to how to rotate it so the image is presented correctly."

So, for load a photo from library taken with iPhone Cammera, you have to do this:

ALAssetRepresentation *assetRep = [photo defaultRepresentation];
CGImageRef imageRef = [assetRep fullResolutionImage];
UIImage *image = [UIImage imageWithCGImage:imageRef scale:[assetRep scale] orientation:[assetRep orientation]];

where photo is an ALAsset object taken from library.

Well, i hope this helps!

lucaslt89
  • 2,431
  • 1
  • 20
  • 30