MonoTouch: Rounded UIImage Convenience Method

后端 未结 5 1790
故里飘歌
故里飘歌 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:38

    Based on BahaiResearch.com answer I've made another method for non-square images and a roundness percentage instead of a literal radius.

    I'm not sure if it will produce ellipsis correctly. So I'll appreciate if some one can test or even improve this method.

    private static UIImage RoundCorners (UIImage image, float roundnessPercentage)
    {
        float width = image.Size.Width;
        float height = image.Size.Height;
        float radius = ((width+height)/2) * (roundnessPercentage/(100*2));
    
        UIGraphics.BeginImageContext (new SizeF (width, height));
        CGContext c = UIGraphics.GetCurrentContext();
    
        c.BeginPath ();
        c.MoveTo(width, height/2);
        //Bottom-right Corner
        c.AddArcToPoint(width, height, height / 2, width, radius);
        //Bottom-left Corner
        c.AddArcToPoint(0, height, 0, 0, radius);
        //Top-left Corner
        c.AddArcToPoint(0, 0, width/2, 0, radius);
        //Top-right Corner
        c.AddArcToPoint(width, 0, width, height/2, radius);
        c.ClosePath();
        c.Clip();
    
        image.Draw (new PointF (0, 0));
        UIImage converted = UIGraphics.GetImageFromCurrentImageContext();
        UIGraphics.EndImageContext ();
        return converted;
    }
    

提交回复
热议问题