我为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;
这是一个小型演示项目。我的情节提要仅包含ImageView,我没有为自己的ImageView设置任何约束。
我不知道为什么会这样,它已经在模拟器和真实设备上进行了测试,但给出了相同的错误
这是演示项目:(非常简单)https://drive.google.com/file/d/0B_poNaia6t8kT0JLSFJleGxEcmc/view?usp=sharing
更新
有人给出的解决方案是将边框颜色从whiteColor更改为clearColor。当然它将使4条线消失。但是,如果我使用clearColor,则无需在ImageView中添加边框。
我出于某些原因需要边框白色
ios - iOS:UIImageView边框半径为白色,在4个角上显示一条奇怪的黑线-LMLPHP

最佳答案

更新代码

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

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;

边框颜色和宽度使用此

迅速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)

输出

ios - iOS:UIImageView边框半径为白色,在4个角上显示一条奇怪的黑线-LMLPHP

更新
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];

迅捷
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)

输出

ios - iOS:UIImageView边框半径为白色,在4个角上显示一条奇怪的黑线-LMLPHP

whole project的代码

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

10-14 20:41