高性能的图片圆角
一般初学者很多情况下设置图片圆角都是设置UIImageView的Layer,
UIImageView* imageView = [[UIImageView alloc]init];
imageView.layer.cornerRadius = 10.0;
imageView.layer.masksToBounds = YES;
但是这回造成离屏渲染耗费性能,下面介绍一种好的处理方式
-(UIImage*)image:(UIImage*)image WithCornerRadius:(CGFloat)radius andSize:(CGSize)size{
CGRect rect = (CGRect){0.f,0.f,size};
UIGraphicsBeginImageContextWithOptions(size, NO, 1.0);
//根据矩形画带圆角的曲线
[[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius]addClip];
[image drawInRect:rect];
UIImage * resultImage = UIGraphicsGetImageFromCurrentImageContext();
//关闭上下文
UIGraphicsEndImageContext();
return resultImage;
}
这是我封装的工具类里的一个方法,直接拖出去用即可,将需要切割的image、圆角半径已经UIImageView的size大小传进去即可。