imgView.layer.cornerRadius = 10;
// 这一行代码是很消耗性能的
imgView.clipsToBounds = YES;**这是离屏渲染(off-screen-rendering),消耗性能的**
给UIImage添加生成圆角图片的扩展API:这是on-screen-rendering
- (UIImage *)imageWithCornerRadius:(CGFloat)radius {
CGRect rect = (CGRect){0.f, 0.f, self.size};
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;
}
本文介绍了一种在iOS中创建圆角图片的方法,通过on-screen-rendering替代了传统的离屏渲染方式,有效降低了性能消耗。文章提供了一个UIImage的扩展API实现,该方法使用Core Graphics来裁剪图片为指定的圆角形状。

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



