4

I'm trying to upload an image from the phone to my web service and am noticing that the Image orientation is lost when I upload the image. Is there something that I need to do before uploading to ensure that the image is uploaded with the correct orientation?

I also looked elsewhere and found objective-C code to Rotate images which I converted over to C#, but every time the rotate method is used, the image turns black i.e. nothing displays I guess.

I am attaching my code for your reference and would really, really appreciate if someone could tell me what I'm doing wrong. Thank you!

    public static UIImage RotateImage(this UIImage image)
    {
        UIImage imageToReturn = null;
        if(image.Orientation == UIImageOrientation.Up)
        {
            imageToReturn = image;
        }
        else 
        {
            CGAffineTransform transform = CGAffineTransform.MakeIdentity();

            switch (image.Orientation) {
                case UIImageOrientation.Down:
                case UIImageOrientation.DownMirrored:
                    transform.Translate(image.Size.Width, image.Size.Height);
                    transform.Rotate((float)Math.PI);
                    break;

                case UIImageOrientation.Left:
                case UIImageOrientation.LeftMirrored:
                    transform.Translate(image.Size.Width, 0);
                    transform.Rotate((float)Math.PI/2);
                    break;

                case UIImageOrientation.Right:
                case UIImageOrientation.RightMirrored:
                    transform.Translate(0, image.Size.Height);
                    transform.Rotate((float)-Math.PI/2);
                    break;
                case UIImageOrientation.Up:
                case UIImageOrientation.UpMirrored:
                    break;
            }

            switch (image.Orientation) {
                case UIImageOrientation.UpMirrored:
                case UIImageOrientation.DownMirrored:
                    transform.Translate(image.Size.Width, 0);
                    transform.Scale(-1, 1);
                    break;

                case UIImageOrientation.LeftMirrored:
                case UIImageOrientation.RightMirrored:
                    transform.Translate(image.Size.Height, 0);
                    transform.Scale(-1, 1);
                    break;
                case UIImageOrientation.Up:
                case UIImageOrientation.Down:
                case UIImageOrientation.Left:
                case UIImageOrientation.Right:
                    break;
            }

            //now draw image
            using(var context = new CGBitmapContext(IntPtr.Zero,
                                                    (int)image.Size.Width, 
                                                    (int)image.Size.Height, 
                                                    image.CGImage.BitsPerComponent,
                                                    image.CGImage.BytesPerRow,
                                                    image.CGImage.ColorSpace,
                                                    image.CGImage.BitmapInfo)){
                context.ConcatCTM(transform);
                switch (image.Orientation) 
                {
                    case UIImageOrientation.Left:
                    case UIImageOrientation.LeftMirrored:
                    case UIImageOrientation.Right:
                    case UIImageOrientation.RightMirrored:
                        // Grr...
                        context.DrawImage(new RectangleF(PointF.Empty,new SizeF(image.Size.Height, image.Size.Width)), image.CGImage);
                        break;
                    default:
                        context.DrawImage(new RectangleF(PointF.Empty, new SizeF(image.Size.Width, image.Size.Height)), image.CGImage);
                        break;
                }

                using(var imageRef = context.ToImage())
                {
                    imageToReturn = new UIImage(imageRef);
                }
            }
        }

        return imageToReturn;
    }
Anup Marwadi
  • 2,517
  • 4
  • 25
  • 42
  • 2
    The fact that the orientation is "lost" by uploading seems to suggest that either the EXIF Orientation tag is no longer attached to the image, or the receiving software does not honor it. Have you explored this possibility? – Jacob Foshee Sep 19 '12 at 21:09
  • I would explore Jacob Foshee's suggestion about the EXIF orientation tag first. See http://stackoverflow.com/questions/9766394/get-exif-data-from-uiimage-uiimagepickercontroller – Diego Dec 27 '12 at 20:06
  • u saved my day. thanks – Anton Tropashko Jul 21 '15 at 10:21

3 Answers3

3

The reason you are getting black images is the translate and rotate calls are backwards. Normal portrait pictures on an iPod/iphone are in 'right' orientation. The correct code to convert them uses:

        case UIImageOrientation.Right:
        case UIImageOrientation.RightMirrored:
            transform.Rotate (-(float)Math.PI / 2);
            transform.Translate (0, input.Size.Height);
            break;

The transform functions are order dependent. This code rotates it into the correct orientation but since rotation is about the 0,0 coordinate in the lower left, the image is now just off the bottom of the frame. the translate then pushes it up where it belongs.

The original code would push the sideways image off the top of the frame, then the rotation would turn things so that the image is turned right but way off to the right of the frame.

Using a small value for height, like 100 or 200 and pi/4 will easily show the differences you get when changing the order of these functions since some of the original image will always be visible.

Random
  • 31
  • 2
1

Using the information provided by @Random I corrected the original code to work:

    private byte[] RotateImage(UIImage image)
    {
        UIImage imageToReturn = null;
        if (image.Orientation == UIImageOrientation.Up)
        {
            imageToReturn = image;
        }
        else
        {
            CGAffineTransform transform = CGAffineTransform.MakeIdentity();

            switch (image.Orientation)
            {
                case UIImageOrientation.Down:
                case UIImageOrientation.DownMirrored:
                    transform.Rotate((float)Math.PI);
                    transform.Translate(image.Size.Width, image.Size.Height);
                    break;

                case UIImageOrientation.Left:
                case UIImageOrientation.LeftMirrored:
                    transform.Rotate((float)Math.PI / 2);
                    transform.Translate(image.Size.Width, 0);
                    break;

                case UIImageOrientation.Right:
                case UIImageOrientation.RightMirrored:
                    transform.Rotate(-(float)Math.PI / 2);
                    transform.Translate(0, image.Size.Height);
                    break;
                case UIImageOrientation.Up:
                case UIImageOrientation.UpMirrored:
                    break;
            }

            switch (image.Orientation)
            {
                case UIImageOrientation.UpMirrored:
                case UIImageOrientation.DownMirrored:
                    transform.Translate(image.Size.Width, 0);
                    transform.Scale(-1, 1);
                    break;

                case UIImageOrientation.LeftMirrored:
                case UIImageOrientation.RightMirrored:
                    transform.Translate(image.Size.Height, 0);
                    transform.Scale(-1, 1);
                    break;
                case UIImageOrientation.Up:
                case UIImageOrientation.Down:
                case UIImageOrientation.Left:
                case UIImageOrientation.Right:
                    break;
            }

            //now draw image
            using (var context = new CGBitmapContext(IntPtr.Zero,
                                                    (int)image.Size.Width,
                                                    (int)image.Size.Height,
                                                    image.CGImage.BitsPerComponent,
                                                    image.CGImage.BytesPerRow,
                                                    image.CGImage.ColorSpace,
                                                    image.CGImage.BitmapInfo))
            {
                context.ConcatCTM(transform);
                switch (image.Orientation)
                {
                    case UIImageOrientation.Left:
                    case UIImageOrientation.LeftMirrored:
                    case UIImageOrientation.Right:
                    case UIImageOrientation.RightMirrored:
                        // Grr...
                        context.DrawImage(new RectangleF(PointF.Empty, new SizeF((float)image.Size.Height, (float)image.Size.Width)), image.CGImage);
                        break;
                    default:
                        context.DrawImage(new RectangleF(PointF.Empty, new SizeF((float)image.Size.Width, (float)image.Size.Height)), image.CGImage);
                        break;
                }

                using (var imageRef = context.ToImage())
                {
                    imageToReturn = new UIImage(imageRef);
                }
            }
        }

        using (NSData imageData = imageToReturn.AsJPEG())
        {
            Byte[] byteArray = new Byte[imageData.Length];
            System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, byteArray, 0, Convert.ToInt32(imageData.Length));
            return byteArray;
        }
    }

Very useful piece of code

JKennedy
  • 18,150
  • 17
  • 114
  • 198
0

I found this gist via this question. Hope this code works out for you!

Community
  • 1
  • 1
EeKay
  • 6,494
  • 3
  • 24
  • 24