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

IOS:UIImageView边框白色带半径在4个角显示一条奇怪的暗线,ios,objective-c,swift,uiimageview,Ios,Objective C,Swift,Uiimageview,我为我的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
设置了白色边框和半径。但在
图像视图的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
设置任何约束

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

下面是演示项目:(非常简单)

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

尝试剪辑到边界:

self.image.clipToBounds = YES
尝试剪辑到边界:

self.image.clipToBounds = YES

我尝试了在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];

我尝试了在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];

当您在图像边框线内添加边框时,以及当您设置圆角边框时,您的圆角具有一些透明部分,因此该部分将显示在拐角侧 只需更改边框颜色

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;

当您在图像边框线内添加边框时,以及当您设置圆角边框时,您的圆角有一些透明部分,因此该部分将显示在角边 只需更改边框颜色

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;

我检查了你的代码,你可以更改这一行,这是我的工作


self.image.layer.borderColor=[[UIColor clearColor]CGColor]

我检查了你的代码,你可以更改这行代码,这是我的工作


self.image.layer.borderColor=[[UIColor clearColor]CGColor]

更新代码

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];
我尝试了你的代码实际上你的图像大小很大最初我根据原始图像大小调整了图像大小

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)
输出

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)

更新

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)
输出

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)


更新代码的代码

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];
我尝试了你的代码实际上你的图像大小很大最初我根据原始图像大小调整了图像大小

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)
输出

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)

更新

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)
输出

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)


实际上,您必须使用两个层

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

实际上你必须使用两层

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

最近得到了同样的问题,但背景略有不同

结果为丑陋的黑色边框:

let image=UIImage(名为:“File.png”)!
让转弯半径:CGFloat=60
let frame=CGRect(原点:.0,大小:image.size)
让路径=UIBezierPath(roundedRect:frame,cornerRadius:cornerRadius)
让rounded=UIGraphicsImageRenderer(大小:image.size).image{context in
path.addClip()
image.draw(在:帧中)
}

但是,如果我们将
.insetBy(dx:1,dy:1)
添加到
框架
,它将解决问题。

最近遇到了相同的问题,但上下文略有不同

结果为丑陋的黑色边框:

let image=UIImage(名为:“File.png”)!
让转弯半径:CGFloat=60
let frame=CGRect(原点:.0,大小:image.size)
让路径=UIBezierPath(roundedRect:frame,cornerRadius:cornerRadius)
让rounded=UIGraphicsImageRenderer(大小:image.size).image{context in
path.addClip()
image.draw(在:帧中)
}


但是,如果我们将
.insetBy(dx:1,dy:1)
添加到
,它将解决问题。

您是否将self.image添加到任何子视图?否,我只是将其拖到我的mainStoryboard@Anbu.Karthik我添加了一个演示项目,如果你不介意的话,你能检查一下吗?实际上你的图像太大了。检查一下这个答案:你把self.image添加到任何子视图了吗?不,我只是把它拖到我的mainStoryboard@Anbu.Karthik我添加了一个演示项目,如果你不介意的话,你能检查一下它吗?你的图像尺寸太大了。检查这个答案:我需要一个白色的边框,not
clearColor
如果您不介意,请检查我的演示项目(我已包含在问题中)我需要白色边框,not
clearColor
如果您不介意,请检查我的演示项目(我已包含在问题中)它不工作。4暗线仍然可见。如果你不介意,请检查我的演示项目(我已经包括在问题中),它不工作。4暗线仍然可见。如果您不介意,请检查我的演示项目(我已包含在问题中),如果我将边框颜色设置为clearColor,那么我不需要将边框添加到我的
ImageView
中,那么您必须将边框宽度设置为0:(很抱歉我的解释不好,但在我的情况下,它需要一个带有
whiteColor
的边框,而不是
clearColor
如果我将边框的颜色设置为clearColor,那么我不需要将边框添加到我的
ImageView
中,那么您必须将边框宽度设置为0:(很抱歉我解释得不好,但就我而言,它需要一个带有
whiteColor
的边框,而不是
clearColor
谢谢,但是你忘记了我的带有
whiteColor
的边框3px。你能把它添加到代码中吗?我已经在代码中添加了它,但它不会显示。)correct@Anbu.Karthik超级感谢你,但你忘了我与whiteCo的border 3pxlor
。您可以将此添加到代码中吗?我已将其添加到代码中,但它不显示correct@Anbu.Karthik超级兄弟