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

我为我的ImageView设置了边框的白色和半径。 但在ImageView四angular,出现了一些黑线。
这是我为我的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设置任何约束。

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

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

更新
有人提出一个解决scheme,就是将边框颜色的颜色从whiteColor改为clearColor 。 当然这会使4条线消失。 但是,如果我使用clearColor ,我不需要添加边框到我的ImageView
出于某种原因,我需要边框白色

在这里输入图像说明

更新的代码

我尝试了你的代码,其实你的图像大小是最初我resize的原始图像大小的图像

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

有时angular落半径不能正常工作,所以我使用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) 

产量

在这里输入图像说明

更新

 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) 

产量

在这里输入图像说明

整个项目的代码

其实你必须使用两层。

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

我检查你的代码,你可以改变这一行对我的工作

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

当你添加一个边框添加到图像边框线中,并且当你设置了圆angular的边框,那么你的圆angular有一些透明的部分,以便部分将显示在angular落边,只是改变边框颜色的颜色

 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; 

检查我的图像有一个白色的边框颜色和一个是清晰的颜色