C# MonoTouch:一种方便的方法

C# MonoTouch:一种方便的方法,c#,ios,xamarin.ios,uiimage,C#,Ios,Xamarin.ios,Uiimage,是否有一种内置的MonoTouch方法来环绕UIImage的边缘 我似乎记得曾经见过它。在MonoTouch(以及iOS本身)中,这不是你可以在UIImage本身上做的事情。但是,您可以通过操纵其层属性在UIImageView上执行此操作 请看下面的Objective-C示例,该示例很容易转换为C。我还建议您阅读以下教程。它涵盖了有关在iOS中自定义层的有趣内容 希望有帮助。将UIImage四舍五入将生成另一个UIImage 如果创建与原始图像大小相同的CGContext进行渲染,然后添加圆角剪

是否有一种内置的MonoTouch方法来环绕
UIImage
的边缘

我似乎记得曾经见过它。

在MonoTouch(以及iOS本身)中,这不是你可以在UIImage本身上做的事情。但是,您可以通过操纵其
属性在
UIImageView
上执行此操作


请看下面的Objective-C示例,该示例很容易转换为C。

我还建议您阅读以下教程。它涵盖了有关在iOS中自定义层的有趣内容


希望有帮助。

将UIImage四舍五入将生成另一个UIImage

如果创建与原始图像大小相同的CGContext进行渲染,然后添加圆角剪切路径并渲染原始图像,则可以执行此操作

然后可以将UIImage从CGContext中拉出

另一个选项是避免中间步骤,即在上下文图形中推送图形状态,添加圆角路径作为剪切路径,绘制图像,然后弹出图形状态以返回到此状态

您可以看到TweetStation如何将其用于玻璃按钮:


这是一个很好的代码助手,适用于寻找快速助手功能的MonoTouch用户。Tweeked代码来自优秀网站:

根据答案,我为非正方形图像制作了另一种方法,用圆度百分比代替文字半径

我不确定它是否能正确地生成省略号。因此,如果有人能够测试甚至改进这种方法,我将不胜感激

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;
}

好的!但无法调整图像大小。。。有什么想法吗?
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;
}