我为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
添加边框
我出于某种原因需要边框白色
答案 0 :(得分:4)
更新了代码
我尝试了你的代码实际上你的图像尺寸很大我最初根据原始图像尺寸重新调整了图像
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)
<强>输出强>
<强>更新强>
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)
<强>输出强>
答案 1 :(得分:1)
实际上你必须使用两层。
self.image.clipsToBounds = YES;
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.image.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(48, 48)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = self.image.bounds;
maskLayer.path = maskPath.CGPath;
maskLayer.strokeColor = [UIColor redColor].CGColor;
self.image.layer.mask = maskLayer;
CAShapeLayer* frameLayer = [CAShapeLayer layer];
frameLayer.frame = self.image.bounds;
frameLayer.path = maskPath.CGPath;
frameLayer.strokeColor = [UIColor whiteColor].CGColor;
frameLayer.fillColor = nil;
frameLayer.lineWidth = 20;
[self.image.layer addSublayer:frameLayer];
答案 2 :(得分:0)
尝试剪辑到界限:
self.image.clipToBounds = YES
答案 3 :(得分:0)
我尝试了用imageview类别编写的这个函数:
- (void)setBorderWithRounCornersWithColor:(UIColor *)color{
self.layer.cornerRadius = 5.0f;
self.layer.masksToBounds = YES;
if(color){
self.layer.borderColor=color.CGColor;
self.layer.borderWidth=1.0f;
}
}
在使用时:
[self.imageView setBorderWithRounCornersWithColor:nil];
答案 4 :(得分:0)
当您添加一个在图像边框线内添加的边框时,当您设置圆角边框时,您的圆角有一些透明部分,以便该部分将显示在角边 只需更改边框颜色的颜色
image1.layer.cornerRadius = 10;
image1.layer.borderWidth = 3;
image1.layer.borderColor = [[UIColor whiteColor] CGColor];
image1.layer.masksToBounds = YES;
image2.layer.cornerRadius = 10;
image2.layer.borderWidth = 3;
image2.layer.borderColor = [[UIColor clearColor] CGColor];
image2.layer.masksToBounds = YES;
答案 5 :(得分:0)
我检查你的代码,你可以改变这条线为我工作
self.image.layer.borderColor = [[UIColor clearColor] CGColor];
答案 6 :(得分:0)
最近遇到了相同的问题,但是情况略有不同。
黑色边框难看的结果:
let image = UIImage(named: "File.png")!
let cornerRadius: CGFloat = 60
let frame = CGRect(origin: .zero, size: image.size)
let path = UIBezierPath(roundedRect: frame, cornerRadius: cornerRadius)
let rounded = UIGraphicsImageRenderer(size: image.size).image { context in
path.addClip()
image.draw(in: frame)
}
但是如果我们将.insetBy(dx: 1, dy: 1)
添加到frame
-将会解决问题。