MonoTouch: Rounded UIImage Convenience Method

后端 未结 5 1750
故里飘歌
故里飘歌 2021-01-21 21:04

Is there a MonoTouch built-in method to round the edges of a UIImage ?

I seem to remember seeing it once.

5条回答
  •  有刺的猬
    2021-01-21 21:30

    Here is a nice code helper for MonoTouch users looking for a quick helper function. Tweeked code came from the Excellent Xamarin.com site:

    public static UIImage RounderCorners (UIImage image, float width, float radius)
    {
        UIGraphics.BeginImageContext (new SizeF (width, width));
        var c = UIGraphics.GetCurrentContext ();
    
                    //Note: You need to write the Device.IsRetina code yourself 
        radius = Device.IsRetina ? radius * 2 : radius;
    
        c.BeginPath ();
        c.MoveTo (width, width / 2);
        c.AddArcToPoint (width, width, width / 2, width, radius);
        c.AddArcToPoint (0, width, 0, width / 2, radius);
        c.AddArcToPoint (0, 0, width / 2, 0, radius);
        c.AddArcToPoint (width, 0, width, width / 2, radius);
        c.ClosePath ();
        c.Clip ();
    
        image.Draw (new PointF (0, 0));
        var converted = UIGraphics.GetImageFromCurrentImageContext ();
        UIGraphics.EndImageContext ();
        return converted;
    }
    

提交回复
热议问题