UIImageView添加圓角
最直接的方法就是使用如下屬性設(shè)置:
imgView.layer.cornerRadius = 10;// 這一行代碼是很消耗性//
imgView.clipsToBounds = YES;//這是離屏渲染(off-screen-rendering)粹庞,消耗性能的使用到了CPU//
給UIImage添加生成圓角圖片的擴(kuò)展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;
}