如何使用圆角和边框宽度去除 UIImageView 上的黑边?

How to remove black edge on UIImageView with rounded corners and a border width?(如何使用圆角和边框宽度去除 UIImageView 上的黑边?)

本文介绍了如何使用圆角和边框宽度去除 UIImageView 上的黑边?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码使我的每个 UITableView 的单元格中的 UIImageView 都有圆角:

I have the following code to make the UIImageView in each of my UITableView's cells have rounded corners:

- (void)awakeFromNib
{
    // Rounded corners.
    [[cellImage layer] setCornerRadius:([cellImage frame].size.height / 2)];
    [[cellImage layer] setMasksToBounds:YES];
    [[cellImage layer] setBorderColor:[[UIColor whiteColor] CGColor]];
    [[cellImage layer] setBorderWidth:3]; // Trouble!
}

我希望图像之间有一点间隙,并认为我可以利用边框宽度来实现这一点.下面是实际发生的图片:

I want the images to have a bit of a gap between them, and figured I could make use of the border width to make that happen. Below is an image of what actually happened:

就是那个微弱的黑色边框,我想知道怎么去掉.我想有一种方法可以使用边框宽度.如果没有,最好的方法可能是调整图像本身的大小并将边框宽度设置为 0.

It's that faint black border that I want to know how to get rid of. I'd like to think there's a way of doing it using border width. If not, the best approach might be just to resize the image itself and just set the border width to be 0.

推荐答案

您可以为蒙版创建贝塞尔路径,为该路径创建一个形状层,然后为图像视图指定该形状层,而不是使用圆角半径图层蒙版:

Rather than using corner radius, you can create bezier path for the mask, create a shape layer for that path, and then specify that shape layer for the image view's layer's mask:

CGFloat margin = 3.0;
CGRect rect = CGRectInset(imageView.bounds, margin, margin);
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(imageView.bounds.size.width/2, imageView.bounds.size.height/2) radius:radius startAngle:0 endAngle:M_PI*2 clockwise:NO];
CAShapeLayer *mask = [CAShapeLayer layer];
mask.path = path.CGPath;
imageView.layer.mask = mask;

这篇关于如何使用圆角和边框宽度去除 UIImageView 上的黑边?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何使用圆角和边框宽度去除 UIImageView 上的黑边?

基础教程推荐