【问题标题】:How to set the rotation angle of the images in ImageView in Xamarin? [duplicate]如何在 Xamarin 的 ImageView 中设置图像的旋转角度? [复制]
【发布时间】:2018-11-16 00:36:42
【问题描述】:

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

【问题讨论】:

    标签: xamarin xamarin.ios


    【解决方案1】:

    尝试参考以下代码

    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,可以参考。

    【讨论】:

    • 有效!谢谢卢卡斯。你和你的同事不时救了我。
    • 不客气。编码愉快:)
    • 不是很开心。我已经加班30个小时不睡觉了。
    【解决方案2】:

    在 Xamarin 中是 CGAffineTransform.MakeRotation

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

    【讨论】:

    • 谢谢!杰森。但我只想旋转图像并重置imageview的图像。
    猜你喜欢
    • 2012-02-17
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    • 1970-01-01
    • 2018-03-27
    相关资源
    最近更新 更多