ClipToBounds没有边框颜色问题-Xamarin.iOS,C#

时间:2018-07-25 13:10:19

标签: c# xamarin xamarin.forms xamarin.ios uiimageview

程序:

  • 我有一个UIImageView。图片视图包含图片,边框和
    拐角半径(圆形)。

  • 图像视图属性“ ClipsToBounds”设置为true,因此图像 匹配圆角。

代码:

ACTION_CREATE_DOCUMENT

问题:

如果我将边框颜色更改为白色(透明),则在每个角都可以看到黑色的细线。 如何摆脱这些?

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:2)

我查看了通过Revel在边界半径上渲染的核心动画层,这些动画层是合成上的渲染工件(我假设是迷你地图/抗锯齿中的舍入误差)原始图像颜色的半径计算中,但这只是一个疯狂的猜测...)

enter image description here

因此,如果使用与您使用的半径相同的半径创建CAShapeLayer并将其应用为图层的蒙版,则伪像会变小。 (“核心动画”贝塞尔曲线路径更准确但并不完美?)

enter image description here

如果您创建的“ CAShapeLayer”在拐角半径处大一个像素作为“ hack”,则工件将被完全遮盖:

enter image description here

CAShapeLayer /遮罩修复示例:

UIImageView imageView = new UIImageView
{
    Image = UIImage.FromBundle("foo.jpg"),
    ClipsToBounds = true,
    Bounds = View.Bounds
};
imageView.Layer.BorderWidth = 5f; //set border thickness
imageView.Layer.CornerRadius = 25; //make corner's rounded
imageView.Layer.BorderColor = UIColor.White.CGColor; //set border color red
var maskingShapeLayer = new CAShapeLayer()
{
    Path = UIBezierPath.FromRoundedRect(imageView.Bounds, UIRectCorner.AllCorners, new CGSize(26, 26)).CGPath
};
imageView.Layer.Mask = maskingShapeLayer;

注意:也许是“核心”核心动画专家的人可以首先解释渲染伪像在这些边界曲线上的存在方式,因为我仍然使用CAShapeLayers进行边​​角遮罩即使在iOS 11+中,您也可以设置UIView的Layer.CornerRadius,就像问题一样。