imgView.layer.cornerRadius =10;// 這一行代碼是很消耗性能的imgView.clipsToBounds = YES;
**這是離屏渲染(off-screen-rendering)嗦玖,消耗性能的**
給UIImage添加生成圓角圖片的擴(kuò)展API:這是on-screen-rendering
- (UIImage*)imageWithCornerRadius:(CGFloat)radius
{
CGRectrect = (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;
}