change UIImageOrientation without scaling

Billy12ShovelsBilly12Shovels USUniversity ✭✭✭

Hello,
So I have a method that Scales and Rotates an image(I found it as a snippet) and I am trying to reconfigure it to just rotate an image and not scale it.
The reason is because I have an image editor that I am having issues with, and I believe that just changing the rotation would fix my problem.
Here is the code snippet:

public static UIImage ScaleAndRotateImage(UIImage imageIn, UIImageOrientation orIn)
    {
        int kMaxResolution = 2048;

        CGImage imgRef = imageIn.CGImage;
        float width = imgRef.Width;
        float height = imgRef.Height;
        CGAffineTransform transform = CGAffineTransform.MakeIdentity();
        RectangleF bounds = new RectangleF(0, 0, width, height);

        if (width > kMaxResolution || height > kMaxResolution)
        {
            float ratio = width / height;

            if (ratio > 1)
            {
                bounds.Width = kMaxResolution;
                bounds.Height = bounds.Width / ratio;
            }
            else
            {
                bounds.Height = kMaxResolution;
                bounds.Width = bounds.Height * ratio;
            }
        }

        float scaleRatio = bounds.Width / width;
        SizeF imageSize = new SizeF(width, height);
        UIImageOrientation orient = orIn;
        float boundHeight;

        switch (orient)
        {
            case UIImageOrientation.Up:                                        //EXIF = 1
                transform = CGAffineTransform.MakeIdentity();
                break;

            case UIImageOrientation.UpMirrored:                                //EXIF = 2
                transform = CGAffineTransform.MakeTranslation(imageSize.Width, 0f);
                transform = CGAffineTransform.MakeScale(-1.0f, 1.0f);
                break;

            case UIImageOrientation.Down:                                      //EXIF = 3
                transform = CGAffineTransform.MakeTranslation(imageSize.Width, imageSize.Height);
                transform = CGAffineTransform.Rotate(transform, (float)Math.PI);
                break;

            case UIImageOrientation.DownMirrored:                              //EXIF = 4
                transform = CGAffineTransform.MakeTranslation(0f, imageSize.Height);
                transform = CGAffineTransform.MakeScale(1.0f, -1.0f);
                break;

            case UIImageOrientation.LeftMirrored:                              //EXIF = 5
                boundHeight = bounds.Height;
                bounds.Height = bounds.Width;
                bounds.Width = boundHeight;
                transform = CGAffineTransform.MakeTranslation(imageSize.Height, imageSize.Width);
                transform = CGAffineTransform.MakeScale(-1.0f, 1.0f);
                transform = CGAffineTransform.Rotate(transform, 3.0f * (float)Math.PI / 2.0f);
                break;

            case UIImageOrientation.Left:                                      //EXIF = 6
                boundHeight = bounds.Height;
                bounds.Height = bounds.Width;
                bounds.Width = boundHeight;
                transform = CGAffineTransform.MakeTranslation(0.0f, imageSize.Width);
                transform = CGAffineTransform.Rotate(transform, 3.0f * (float)Math.PI / 2.0f);
                break;

            case UIImageOrientation.RightMirrored:                             //EXIF = 7
                boundHeight = bounds.Height;
                bounds.Height = bounds.Width;
                bounds.Width = boundHeight;
                transform = CGAffineTransform.MakeScale(-1.0f, 1.0f);
                transform = CGAffineTransform.Rotate(transform, (float)Math.PI / 2.0f);
                break;

            case UIImageOrientation.Right:                                     //EXIF = 8
                boundHeight = bounds.Height;
                bounds.Height = bounds.Width;
                bounds.Width = boundHeight;
                transform = CGAffineTransform.MakeTranslation(imageSize.Height, 0.0f);
                transform = CGAffineTransform.Rotate(transform, (float)Math.PI / 2.0f);
                break;

            default:
                throw new Exception("Invalid image orientation");
                break;
        }

        UIGraphics.BeginImageContext(bounds.Size);

        CGContext context = UIGraphics.GetCurrentContext();

        if (orient == UIImageOrientation.Right || orient == UIImageOrientation.Left)
        {
            context.ScaleCTM(-scaleRatio, scaleRatio);
            context.TranslateCTM(-height, 0);
        }
        else
        {
            context.ScaleCTM(scaleRatio, -scaleRatio);
            context.TranslateCTM(0, -height);
        }

        context.ConcatCTM(transform);
        context.DrawImage(new RectangleF(0, 0, width, height), imgRef);

        UIImage imageCopy = UIGraphics.GetImageFromCurrentImageContext();
        UIGraphics.EndImageContext();

        return imageCopy;
    }

If anybody could tell me how to re-factor it, and why to get rid of some parts, that would be appreciated.
Thanks!

Tagged:
Sign In or Register to comment.