图片设置圆角

关于给图片设置圆角,普遍使用layer进行处理,简单方便;
但过多的渲染layer会导致降低app性能,拖慢了fps(特别是在cell中容易发觉)。

建议通过渲染画布的方式实现:

分类:
@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

你可能感兴趣的:(图片设置圆角)