c# - ClipToBounds 没有边框颜色问题 - Xamarin.iOS,C#
问题描述
程序:
我有一个 UIImageView。图像视图具有图像、边框和
圆角半径(圆角)。图像视图属性“ClipsToBounds”设置为 true,因此图像与圆角匹配。
代码:
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
问题:
如果我将边框颜色更改为白色(透明),您可以在每个角落看到小黑线。我怎样才能摆脱这些?
解决方案
我查看了通过 Revel 在边界半径上渲染的核心动画层,那些是合成上的渲染伪影(我假设这些是原始图像颜色的半径计算中的迷你地图/抗锯齿中的舍入误差,但这只是一个疯狂的猜测......)
因此,如果您创建一个CAShapeLayer
与您正在使用的半径相同的并将其应用为图层的蒙版,则工件会变得更小。(Core Animation 贝塞尔路径更准确但不完美?)
如果您创建一个在拐角半径上大一个像素的 CAShapeLayer 作为“hack”,则工件将被完全掩盖:
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 。
推荐阅读
- r - 如何使用 R 中的循环将“j”行分配给具有不同模式的“i”行?
- ibm-midrange - 在 Db2 for i 查询结果中,连字符表示什么?
- svg - 如何正确导入 svg 作为 VueComponent
- kendo-ui - 按下“最小化”按钮时更改剑道窗口的大小
- c# - 使用 SharpDX.Direct3D11 进行屏幕截图时,找不到适合该区域的输出设备
- java - 如何将 Zebra TC520K 触摸式计算机扫描仪限制为仅扫描 9 位条形码?
- java - 如何从java中的lua脚本中获取本地人
- javascript - 我可以将父容器设置为溢出:隐藏,但只有特定的子元素表现得好像父容器溢出:可见?
- azure-functions - 主题不包含字符串的 Azure EventGrid 高级筛选器
- apache-flink - Flink 设置 Timer 和广播流