0

I am working with Xamarin.iOS and now I want to rotate the image 90 degrees in ImageView when I click button.I found a similar issue.But it wrote in OC and Swift.How to implement it in C#?I couldn't find method such as CGAffineTransformMakeRotation.

2 Answers2

2

in Xamarin it's CGAffineTransform.MakeRotation

using CoreGraphics;

// 1.5708 is 90 degrees in Radians
myImageView.Transform = CGAffineTransform.MakeRotation(1.5708f);
Jason
  • 86,222
  • 15
  • 131
  • 146
2

Try to refer the following code

public UIImage RotateImage(UIImage image, float degree)
{
  float Radians = degree * (float)Math.PI / 180;

  UIView view = new UIView(frame: new CGRect(0, 0, image.Size.Width, image.Size.Height));
  CGAffineTransform t = CGAffineTransform.MakeRotation(Radians);
  view.Transform = t;
  CGSize size = view.Frame.Size;

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

  context.TranslateCTM(size.Width/2, size.Height/2);
  context.RotateCTM(Radians);
  context.ScaleCTM(1, -1);

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

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

  return imageCopy;
}

Then you can reset the image of UIImageView. Here is a similar issue you can refer .

Lucas Zhang
  • 18,630
  • 3
  • 12
  • 22