iOS高效的设置圆角效果

普通的UIView可直接设置cornerRadius属性即可。
遇到需要使用masksToBounds属性(例如UIImageView)可以使用以下这种方法:
- (void)kt_addImageViewCorner:(CGFloat)radius{
self.image = [self kt_drawRectWithRoundedCorner:radius sizetoFit:self.bounds.size];
}
- (UIImage *)kt_drawRectWithRoundedCorner:(CGFloat)radius sizetoFit:(CGSize)sizetoFit{
CGRect rect = CGRectMake(0, 0, sizetoFit.width, sizetoFit.height);
UIGraphicsBeginImageContextWithOptions(rect.size, false,[UIScreen mainScreen].scale);
CGContextAddPath(UIGraphicsGetCurrentContext(),[UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(radius, radius)].CGPath);
CGContextClip(UIGraphicsGetCurrentContext());
[self drawRect:rect];
CGContextDrawPath(UIGraphicsGetCurrentContext(), kCGPathFillStroke);
UIImage *output = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return output;

}

你可能感兴趣的:(iOS优化)