2

I have a class that represents a quote form. I have added a button to take a picture and then add that as an attachment from within the app. In my imageView, the picture shows portrait (as it was taken that way), but in the email attachment, it gets rotated 90° so that it is landscape. How can I fix this?

fmi
  • 512
  • 4
  • 14
  • 1
    Images taken with the iOS camera have an imageOrientation property set based on the orientation of the device when the picture is taken. Lot's of apps don't support this property so the image looks rotated. You correct this by rotating the image after it's taken. Search on SO for rotating/orienting images taken with UIImagePickerController. There's plenty of help on this topic here. – XJones Nov 24 '11 at 02:03
  • The only view available in the app is portrait so I'm surprised that it would show anything else. I'll search based on your criteria...thanks – fmi Nov 24 '11 at 02:07
  • the orientation of the app doesn't matter. when you take a pic using `UIImagePickerController`, `imageOrientation` is set based on how you are holding the device when you take the pic. – XJones Nov 24 '11 at 02:10
  • @XJones I'm holding the device portrait, and the imageView shows portrait...but it is rotated (landscape) in my email attachment. – fmi Nov 24 '11 at 13:17
  • I tried to point you in the right direction with the `imageOrientation` property. Here's one of the many SO questions on this topic. The answer looks decent. http://stackoverflow.com/questions/5427656/ios-uiimagepickercontroller-result-image-orientation-after-upload – XJones Nov 24 '11 at 17:25
  • You'll find another answer to this question [here](http://stackoverflow.com/questions/20204495/mfmailcomposeviewcontroller-image-orientation). – Gallymon Nov 29 '13 at 00:10

1 Answers1

2

I have also faced same problem but this code solves it

- (UIImage*)imageWithImage:(UIImage*)sourceImage scaledToSizeWithSameAspectRatio:(CGSize)targetSize
{  
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);

    if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;

        if (widthFactor > heightFactor) {
            scaleFactor = widthFactor; // scale to fit height
        }
        else {
            scaleFactor = heightFactor; // scale to fit width
        }

        scaledWidth  = width * scaleFactor;
        scaledHeight = height * scaleFactor;

        // center the image
        if (widthFactor > heightFactor) {
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 
        }
        else if (widthFactor < heightFactor) {
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }
    }     

    CGImageRef imageRef = [sourceImage CGImage];
    CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
    CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);

    if (bitmapInfo == kCGImageAlphaNone) {
        bitmapInfo = kCGImageAlphaNoneSkipLast;
    }

    CGContextRef bitmap;

    if (sourceImage.imageOrientation == UIImageOrientationUp || sourceImage.imageOrientation == UIImageOrientationDown) {
        bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);

    } else {
        bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);

    }   

    // In the right or left cases, we need to switch scaledWidth and scaledHeight,
    // and also the thumbnail point
    if (sourceImage.imageOrientation == UIImageOrientationLeft) {
        thumbnailPoint = CGPointMake(thumbnailPoint.y, thumbnailPoint.x);
        CGFloat oldScaledWidth = scaledWidth;
        scaledWidth = scaledHeight;
        scaledHeight = oldScaledWidth;

        CGContextRotateCTM (bitmap, radians(90));
        CGContextTranslateCTM (bitmap, 0, -targetHeight);

    } else if (sourceImage.imageOrientation == UIImageOrientationRight) {
        thumbnailPoint = CGPointMake(thumbnailPoint.y, thumbnailPoint.x);
        CGFloat oldScaledWidth = scaledWidth;
        scaledWidth = scaledHeight;
        scaledHeight = oldScaledWidth;

        CGContextRotateCTM (bitmap, radians(-90));
        CGContextTranslateCTM (bitmap, -targetWidth, 0);

    } else if (sourceImage.imageOrientation == UIImageOrientationUp) {
        // NOTHING
    } else if (sourceImage.imageOrientation == UIImageOrientationDown) {
        CGContextTranslateCTM (bitmap, targetWidth, targetHeight);
        CGContextRotateCTM (bitmap, radians(-180.));
    }

    CGContextDrawImage(bitmap, CGRectMake(thumbnailPoint.x, thumbnailPoint.y, scaledWidth, scaledHeight), imageRef);
    CGImageRef ref = CGBitmapContextCreateImage(bitmap);
    UIImage* newImage = [UIImage imageWithCGImage:ref];

    CGContextRelease(bitmap);
    CGImageRelease(ref);
    NSLog(@"sourceImage:%i",sourceImage.imageOrientation);
    NSLog(@"newImage:%i",newImage.imageOrientation);




    return newImage; 
}
Rahul Chavan
  • 480
  • 3
  • 6
  • Thanks for the help, but it isn't working out for me. My image shows that it is still landscape in the attachment. Do I need to change one of the variables? – fmi Nov 29 '11 at 02:21
  • You can change the size of original image – Rahul Chavan Nov 29 '11 at 06:25