如何在Xamarin中的ImageView中设置图像的旋转角度?

时间:2018-11-16 00:36:42

标签: xamarin xamarin.ios

我正在使用Xamarin.iOS,现在我想在单击按钮时将图像在ImageView中旋转90度。我发现了类似的issue。但是它是用OC和Swift编写的。如何在C#?找不到CGAffineTransformMakeRotation之类的方法。

2 个答案:

答案 0 :(得分:2)

尝试引用以下代码

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;
}

然后,您可以重置UIImageView的图像。 这是a similar issue,您可以参考。

答案 1 :(得分:1)

在Xamarin中是CGAffineTransform.MakeRotation

using CoreGraphics;

// 1.5708 is 90 degrees in Radians
myImageView.Transform = CGAffineTransform.MakeRotation(1.5708f);