1.直接设置
iOS9之后不会触发离屏渲染(off-screen-rendering)
imgView.layer.cornerRadius = 50;
imgView..layer.masksToBounds= YES;
2.对图片重绘
给UIImage添加生成圆角图片的分类,性能最好
- (UIImage *)imageWithCornerRadius:(CGFloat)radius {
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
//开启图片上下文
UIGraphicsBeginImageContextWithOptions(self.size, NO, UIScreen.mainScreen.scale);
// 画圆
CGContextAddPath(UIGraphicsGetCurrentContext(),[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius].CGPath);
//裁剪
CGContextClip(UIGraphicsGetCurrentContext());
//绘制
[self drawInRect:rect];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
3.使用CAShapeLayer,实际测试会有离屏渲染,
// 头像圆角
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.portarit.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:self.portarit.bounds.size];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
maskLayer.frame = self.portarit.bounds;
maskLayer.path = maskPath.CGPath;
self.portarit.layer.mask = maskLayer;
3.总结
图片圆角没什么难度,一般情况下用第1个方法就可以了, 不过当在tableView中最好使用第2个方法
网友评论