【发布时间】:2018-11-16 00:36:42
【问题描述】:
我正在使用 Xamarin.iOS,现在我想在单击按钮时将 ImageView 中的图像旋转 90 度。我找到了类似的issue。但它是用 OC 和 Swift 编写的。如何在 C# 中实现它?找不到CGAffineTransformMakeRotation之类的方法。
【问题讨论】:
标签: xamarin xamarin.ios
我正在使用 Xamarin.iOS,现在我想在单击按钮时将 ImageView 中的图像旋转 90 度。我找到了类似的issue。但它是用 OC 和 Swift 编写的。如何在 C# 中实现它?找不到CGAffineTransformMakeRotation之类的方法。
【问题讨论】:
标签: xamarin xamarin.ios
尝试参考以下代码
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,可以参考。
【讨论】:
在 Xamarin 中是 CGAffineTransform.MakeRotation
using CoreGraphics;
// 1.5708 is 90 degrees in Radians
myImageView.Transform = CGAffineTransform.MakeRotation(1.5708f);
【讨论】: