gpt4 book ai didi

IOS:带有半径的 UIImageView 边框白色在 4 个角显示一条奇怪的黑线

转载 作者:搜寻专家 更新时间:2023-11-01 06:06:24 26 4
gpt4 key购买 nike

我为我的 ImageView 设置了白色边框和半径。但是在 ImageView 的第 4 个角,出现了一些暗线。
这是我为 ImageView

设置颜色的代码
self.image.layer.cornerRadius = 10;
self.image.layer.borderWidth = 3;
self.image.layer.borderColor = [[UIColor whiteColor] CGColor];
self.image.layer.masksToBounds = YES;

这是一个小型演示项目。我的 Storyboard仅包含 ImageView,并且我没有为我的ImageView 设置任何约束。

我不知道为什么会这样,它已经在模拟器和真实设备上进行了测试,但它给出了同样的错误

这是演示项目:(非常简单)https://drive.google.com/file/d/0B_poNaia6t8kT0JLSFJleGxEcmc/view?usp=sharing

更新
有人给出的解决方案是将边框颜色的颜色从 whiteColor 更改为 clearColor。当然会让4行消失。但是,如果我使用 clearColor,则不需要向我的 ImageView 添加边框。
出于某种原因我需要白色边框

enter image description here

最佳答案

更新代码

我试过你的代码,实际上你的图片尺寸很大,最初我根据原始图片尺寸调整了图片的大小

UIImage *myIcon = [self imageWithImage:[UIImage imageNamed:@"abc.jpg"] scaledToSize:CGSizeMake(400, 400)];
self.image.image = myIcon;

有时角半径不能正常工作,所以我使用 UIBezierPath 来实现这个概念

 UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:self.image.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(10.0, 10.0)];

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.view.bounds;
maskLayer.path = maskPath.CGPath;
self.image.layer.mask = maskLayer;

边框颜色和宽度使用这个

swift 3

let maskPath = UIBezierPath(roundedRect: imageView.bounds, byRoundingCorners: ([.topLeft, .topRight, .bottomLeft, .bottomRight]), cornerRadii: CGSize(width: 10.0, height: 10.0))


let borderShape = CAShapeLayer()
borderShape.frame = self.imageView.bounds
borderShape.path = maskPath.cgPath
borderShape.strokeColor = UIColor.white.cgColor
borderShape.fillColor = nil
borderShape.lineWidth = 3
self.imageView.layer.addSublayer(borderShape)

输出

enter image description here

更新

CAShapeLayer*   borderShape = [CAShapeLayer layer];
borderShape.frame = self.image.bounds;
borderShape.path = maskPath.CGPath;
borderShape.strokeColor = [UIColor whiteColor].CGColor;
borderShape.fillColor = nil;
borderShape.lineWidth = 3;
[self.image.layer addSublayer:borderShape];

swift

var borderShape: CAShapeLayer = CAShapeLayer.layer
borderShape.frame = self.image.bounds
borderShape.path = maskPath.CGPath
borderShape.strokeColor = UIColor.whiteColor().CGColor
borderShape.fillColor = nil
borderShape.lineWidth = 3
self.image.layer.addSublayer(borderShape)

输出

enter image description here

whole project 的代码

关于IOS:带有半径的 UIImageView 边框白色在 4 个角显示一条奇怪的黑线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36174991/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com