简体   繁体   English

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

[英]ClipToBounds with no border colour issue - Xamarin.iOS, C#

Program: 程序:

  • I have a UIImageView. 我有一个UIImageView。 The image view has an image, a border and a 图像视图具有图像,边框和
    corner radius (rounded). 角半径(圆形)。

  • The image view property "ClipsToBounds" is set to true so the image matches the rounded corners. 图像视图属性“ ClipsToBounds”设置为true,因此图像与圆角匹配。

Code: 码:

UIImageView imageView = new UIImageView(); 
imageView.Layer.BorderWidth = 5f; //set border thickness
imageView.Layer.CornerRadius = 25; //make corner's rounded
imageView.Layer.BorderColor = UIColor.Red.CGColor; //set border color red
imageView.Image = new UIImage(imgPath); //set img
imageView.ClipsToBounds = true; //required - so image is rounded

Problem: 问题:

If I change the border colour to white (transparent) you can see small black lines at every corner. 如果将边框颜色更改为白色(透明),则在每个角处都可以看到黑色的细线。 How can I get rid of these? 我该如何摆脱这些?

在此处输入图片说明

在此处输入图片说明

I looked at the core animation layers that being rendered via Revel on the border radius and those are rendering artifacts on the composite (I assume those are rounding errors in the mini-map/anti-aliasing in the radius calculations of the original image color, but that is just a wild guess...) 我查看了通过Revel在边框半径上渲染的核心动画层,它们是合成上的渲染伪像(我假设这些是迷你图/原始图像半径计算中的抗锯齿化中的舍入误差,但这只是一个疯狂的猜测...)

在此处输入图片说明

So if you create a CAShapeLayer with the same radius you are using and apply it as the layer's mask, the artifact becomes smaller. 因此,如果使用与您使用的半径相同的半径创建CAShapeLayer并将其应用为图层的蒙版,则工件会变小。 (The Core Animation bezier path is more accurate but not perfect?) (“核心动画”贝塞尔曲线路径更准确但并不完美?)

在此处输入图片说明

If you create a CAShapeLayer that is one pixel larger on the corner's radius as a "hack", the artifacts are totally masked: 如果您创建的“ CAShapeLayer”在拐角处的半径大一个像素作为“ hack”,则工件将被完全掩盖:

在此处输入图片说明

CAShapeLayer/Mask Fix-up Example: 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;

Note: Maybe someone that is a "hard-core" Core Animation expert can explain way the rendering artifacts exist on these border curves in the first place as I still use CAShapeLayers to do my corner masking which do not have the rendering artifacts even though in iOS 11+ you can just set your UIView's Layer.CornerRadius like the question does. 注意:也许是“核心” Core Animation专家的人可以首先解释渲染伪像在这些边界曲线上的存在方式,因为我仍然使用CAShapeLayers进行角遮罩,即使在在iOS 11或更高版本中,您可以像问题一样设置UIView的Layer.CornerRadius。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM