2016-03-23 27 views
1

我为我的ImageView设置了边框白色和半径。但是在ImageView的四角出现了一些黑线。
这里是我的代码设置颜色为我ImageViewIOS:UIImageView带有半径的白色边框在四角显示一条奇怪的黑线

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添加边框。
我需要边框白色出于某种原因

enter image description here

+0

你添加的self.image任何子视图 –

+0

没有,我只是把它拖到我mainStoryboard –

+0

@ Anbu.Karthik我有添加一个示范项目,如果你不介意的话,您可以检查它 –

回答

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; 

对于边框的颜色和宽度使用此

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

斯威夫特

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

+0

谢谢你,但是你忘记了我的边框3px与'whiteColor'。你可以加入这个代码。我已经添加它在我的代码,但它不't显示正确 –

+0

感谢兄弟,它工作得很好 –

+0

@ Anbu.Karthik超级兄弟 –

0

尝试剪辑范围:

self.image.clipToBounds = YES 
+0

即无法使用。 4暗线仍然可见 –

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

它不工作。 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; 

Check my image one has a white border color and one is clear color

+0

我需要一个白色的边框,而不是'clearColor' 如果你不介意的话,请检查我的演示项目我已经包括在这个问题中) –

0

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

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

+0

如果我将边框的颜色设置为clearColor,比我不需要将边框添加到我的ImageView中 –

+0

那么你必须设置边框宽度为0 – Birendra

+0

:(对不起,我的坏解释,但在我的情况下,它需要一个'whiteColor'的边框而不是'clearColor' –

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

谢谢,它真的很有帮助 –