第一种方法:通过设置layer的属性
这种方法简单,但是很影响性能,特别是在UIcollectionView中展示大量圆角图片,一般在正常的开发中使用很少
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
//设置圆角
imageView.layer.cornerRadius = imageView.frame.size.width / 2;
//将多余的部分切掉
imageView.layer.masksToBounds = YES;
[self.view addSubview:imageView];
第二种方法:使用CAShapeLayer和UIBezierPath设置圆角,第二种最好,对内存的消耗最少,而且渲染快速
UIImageView *view = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view];
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectEdgeLeft |
UIRectCornerBottomLeft cornerRadii:CGSizeMake(10, 10)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
maskLayer.frame = view.bounds;
maskLayer.path = path.CGPath;
view.layer.mask = maskLayer;
UIRectEdgeLeft 可以根据需求设置需要设置圆角的位置。

本文介绍了两种在iOS应用中实现圆角图片的方法:通过设置layer的属性和使用CAShapeLayer与UIBezierPath。前者虽然简单但性能消耗较大,后者则更高效且渲染速度更快。
1万+

被折叠的 条评论
为什么被折叠?



