如何在Xamarin的ImageView中设置图像的旋转角度?
我正在使用Xamarin.iOS,现在我想在单击按钮时在ImageView中将图像旋转90度。我发现了一个类似的。但它是用OC和Swift编写的。如何在C#中实现它?我在Xamarin中找不到类似的方法,例如如何在Xamarin的ImageView中设置图像的旋转角度?,xamarin,xamarin.ios,Xamarin,Xamarin.ios,我正在使用Xamarin.iOS,现在我想在单击按钮时在ImageView中将图像旋转90度。我发现了一个类似的。但它是用OC和Swift编写的。如何在C#中实现它?我在Xamarin中找不到类似的方法,例如CGAffineTransformMakeRotation 在Xamarin是 请尝试引用以下代码 public UIImage RotateImage(UIImage image, float degree) { float Radians = degree * (float)Math
CGAffineTransformMakeRotation
在Xamarin是
请尝试引用以下代码
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的图像。
以下是您可以参考的代码。请尝试参考以下代码
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的图像。
这是您可以参考的。谢谢!杰森。但我只想旋转图像并重置imageview的图像。谢谢!杰森。但我只想旋转图像并重置imageview的图像。它可以工作!谢谢你,卢卡斯。你和你的同事们时不时地救了我。不客气。快乐编码:)不太快乐。我一直在加班30个小时不睡觉。这很有效!谢谢你,卢卡斯。你和你的同事不时救我。不客气。快乐编码:)不太快乐。我一直在加班30个小时,没有睡觉。