1.直接設(shè)置
iOS9之后不會觸發(fā)離屏渲染(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.總結(jié)
圖片圓角沒什么難度,一般情況下用第1個方法就可以了, 不過當(dāng)在tableView中最好使用第2個方法