如何删除UIImageView上带有圆角和边框宽度的黑边? [英] How to remove black edge on UIImageView with rounded corners and a border width?

查看:779
本文介绍了如何删除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上带有圆角和边框宽度的黑边?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆