0

I have a UIImage which comes from the Camera Roll. I have the following code:

UIImage *img = [UIImage imageWithCGImage: [[asset defaultRepresentation] fullResolutionImage]];
return UIImageJPEGRepresentation(img, 1.0);

When I view img in an UIImageView the orientation is correct. However, after I return the NSData object from UIImageJPEGRepresentation some images appear upside down (not all of them, just some of them).

I've found code that should fix it but it doesn't work for me because the ones that appear upside down return an orientation of UP, so the code doesn't execute. The code that I've tried to use is:

if (image.imageOrientation == UIImageOrientationUp) return image;
CGAffineTransform transform = CGAffineTransformIdentity;

switch (image.imageOrientation) {
    case UIImageOrientationDown:
    case UIImageOrientationDownMirrored:
        transform = CGAffineTransformTranslate(transform, image.size.width, image.size.height);
        transform = CGAffineTransformRotate(transform, M_PI);
        break;

    case UIImageOrientationLeft:
    case UIImageOrientationLeftMirrored:
        transform = CGAffineTransformTranslate(transform, image.size.width, 0);
        transform = CGAffineTransformRotate(transform, M_PI_2);
        break;

    case UIImageOrientationRight:
    case UIImageOrientationRightMirrored:
        transform = CGAffineTransformTranslate(transform, 0, image.size.height);
        transform = CGAffineTransformRotate(transform, -M_PI_2);
        break;
    case UIImageOrientationUp:
    case UIImageOrientationUpMirrored:
        break;
}

switch (image.imageOrientation) {
    case UIImageOrientationUpMirrored:
    case UIImageOrientationDownMirrored:
        transform = CGAffineTransformTranslate(transform, image.size.width, 0);
        transform = CGAffineTransformScale(transform, -1, 1);
        break;

    case UIImageOrientationLeftMirrored:
    case UIImageOrientationRightMirrored:
        transform = CGAffineTransformTranslate(transform, image.size.height, 0);
        transform = CGAffineTransformScale(transform, -1, 1);
        break;
    case UIImageOrientationUp:
    case UIImageOrientationDown:
    case UIImageOrientationLeft:
    case UIImageOrientationRight:
        break;
}

// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
CGContextRef ctx = CGBitmapContextCreate(NULL, image.size.width, image.size.height,
                                         CGImageGetBitsPerComponent(image.CGImage), 0,
                                         CGImageGetColorSpace(image.CGImage),
                                         CGImageGetBitmapInfo(image.CGImage));
CGContextConcatCTM(ctx, transform);
switch (image.imageOrientation) {
    case UIImageOrientationLeft:
    case UIImageOrientationLeftMirrored:
    case UIImageOrientationRight:
    case UIImageOrientationRightMirrored:
        // Grr...
        CGContextDrawImage(ctx, CGRectMake(0,0,image.size.height,image.size.width), image.CGImage);
        break;

    default:
        CGContextDrawImage(ctx, CGRectMake(0,0,image.size.width,image.size.height), image.CGImage);
        break;
}

// And now we just create a new UIImage from the drawing context
CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImage imageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
return img;

Any ideas why UIImageJPEGRepresentation would affect the orientation?

Thanks in advance.

Jacob Joz
  • 732
  • 1
  • 11
  • 29

1 Answers1

-1

Because the UIImageJPEGRepresentation of a UIImage discards much of the UIImage metadata, including the imageOrientation info. You have to add the orientation info back after converting to JPEG but before you store the image. Otherwise, all your JPEGs will report that they are oritented Up.

Now how to actually add that, I don't know... start with How to write orientation data to the UIImage iOS SDK and Problem setting exif data for an image and iOS UIImagePickerController result image orientation after upload

Community
  • 1
  • 1
Ben Wheeler
  • 6,788
  • 2
  • 45
  • 55