關(guān)于給圖片設(shè)置圓角,普遍使用layer進(jìn)行處理,簡單方便;
但過多的渲染layer會導(dǎo)致降低app性能,拖慢了fps(特別是在cell中容易發(fā)覺)。
建議通過渲染畫布的方式實現(xiàn):
分類:
@implementation UIImage (RoundedCorner)
- (UIImage *)imageWithRoundedCornersAndSize:(CGSize)sizeToFit a ndCornerRadius:(CGFloat)radius {
CGRect rect = (CGRect){0.f, 0.f, sizeToFit};
UIGraphicsBeginImageContextWithOptions(sizeToFit, NO, UIScr een.mainScreen.scale);
CGContextAddPath(UIGraphicsGetCurrentContext(),
[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius].CGPath);
CGContextClip(UIGraphicsGetCurrentContext());
[self drawInRect:rect];
UIImage *output = UIGraphicsGetImageFromCurrentImageContext ();
return output
}
- (void)drawRect:(CGRect)rect {
CGRect bounds = self.bounds;
[[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:8.0] addClip];[self.image drawInRect:bounds];
}
@end