如何删除带有圆角和边框宽度的 UIImageView 上的黑边?

How to remove black edge on UIImageView with rounded corners and a border width?

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

- (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!
}

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

我想知道如何去掉那条模糊的黑色边框。我想有一种方法可以使用边框宽度来做到这一点。如果没有,最好的方法可能只是调整图像本身的大小并将边框宽度设置为 0。

你所做的是,你正在将个人资料图片和边框剪裁在一起,这使得单元格的个人资料图片延伸到边框之外。

您错过了这一行:-

cellImage.clipsToBounds = YES;

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

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;