0

I try to rotate image and save it, when i save image as it, it works, but when i try to rotate the image, rotation works but image is blank.

  public UIImage RotateImage (UIImage originalImage, int rotationAngle)
        {
            UIImage rotatedImage = originalImage;

            if (rotationAngle > 0) {
                CGSize rotatedSize;
                float angle = Convert.ToSingle ((Math.PI / 180) * rotationAngle);

                using (UIView rotatedViewBox = new UIView (new CGRect (0, 0, originalImage.Size.Width, originalImage.Size.Height))) {
                    CGAffineTransform t = CGAffineTransform.MakeRotation (angle);
                    rotatedViewBox.Transform = t;
                    rotatedSize = rotatedViewBox.Frame.Size;

                    UIGraphics.BeginImageContext (rotatedSize);
                    CGContext context = UIGraphics.GetCurrentContext ();

                    context.TranslateCTM (rotatedSize.Width / 2, rotatedSize.Height / 2);
                    context.RotateCTM (angle);
                    context.ScaleCTM ((nfloat)1.0, -(nfloat)1.0);

                    context.DrawImage (new CGRect (-originalImage.Size.Width / 2, -originalImage.Size.Height / 2, originalImage.Size.Width, originalImage.Size.Height), originalImage.CGImage);

                    rotatedImage = UIGraphics.GetImageFromCurrentImageContext ();

                    UIGraphics.EndImageContext ();
                }

            }

            return rotatedImage;
        }

Here is what i get :

Rotated image

And original image :

original image

update : little code change

Leze
  • 739
  • 5
  • 24
  • I think the image is blank because there is no relation defined between the image and the context created. – NeverHopeless Oct 04 '16 at 11:04
  • As you can see in code (commented) i try to link us by context.DrawImage but with no success. Then i try with originalImage.Draw but again blank image. – Leze Oct 04 '16 at 11:55
  • Would you be kind to clarify the problem you have? The code you shared rotates image just well, I've checked it with my test application. I would also suggest to use `UIGraphics.BeginImageContext(rotatedSize)` within `using()`. – Alex Sorokoletov Oct 05 '16 at 17:14
  • With this code only the viewport of the image is rotated, the image herself is not, at least on my app (look at screenshot) – Leze Oct 06 '16 at 14:36

1 Answers1

1

rotate image using CGContextDrawImage

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

        float angle = Convert.ToSingle((rotationAngle / 180f) * Math.PI);
        transform = CGAffineTransform.MakeRotation(angle);

        UIGraphics.BeginImageContext(bounds.Size);

        CGContext context = UIGraphics.GetCurrentContext();

        context.TranslateCTM(width / 2, height / 2);
        context.SaveState();
        context.ConcatCTM(transform);
        context.SaveState();
        context.ConcatCTM(CGAffineTransform.MakeScale(1.0f, -1.0f));

        context.DrawImage(new RectangleF(-width / 2, -height / 2, width, height), imgRef);
        context.RestoreState();

        UIImage imageCopy = UIGraphics.GetImageFromCurrentImageContext();
        UIGraphics.EndImageContext();
Fr100
  • 11
  • 1