UIImageView简单的性能优化

2018-08-17  本文已影响8人  farmerly

看代码吧~

分类: UIImageView+Extension.h
/*
 设置圆角优化图片性能
 UIRectCornerTopLeft     = 1 << 0,
 UIRectCornerTopRight    = 1 << 1,
 UIRectCornerBottomLeft  = 1 << 2,
 UIRectCornerBottomRight = 1 << 3,
 UIRectCornerAllCorners  = ~0UL
 radiusSize     设置圆角大小(按照图片大小计算)
 cornerPosition 设置圆角位置
 */
- (void)imageOptimizePerformance:(CGSize)radiusSize 
  cornerPosition:(UIRectCorner)cornerPosition;
分类: UIImageView+Extension.m
 - (void)imageOptimizePerformance:(CGSize)radiusSize       
    cornerPosition:(UIRectCorner)cornerPosition{

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:cornerPosition cornerRadii:radiusSize];

CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
//设置大小
maskLayer.frame = self.bounds;
//设置图形样子
maskLayer.path = maskPath.CGPath;
self.layer.mask = maskLayer;
}
一次性要裁剪四个脚,混合图层,图片多了会导致异常卡顿
优化前.png

优化前

UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(60, 60, 100, 100)];
imageView.layer.cornerRadius = 20;
imageView.layer.masksToBounds = YES;
[imageView sd_setImageWithURL:[NSURL URLWithString:@"http://img.zcool.cn/community/0117e2571b8b246ac72538120dd8a4.jpg"]];
[self.view addSubview:imageView];

优化后


优化后.png
 UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(60, 60, 100, 100)];、
[imageView sd_setImageWithURL:[NSURL URLWithString:@"http://img.zcool.cn/community/0117e2571b8b246ac72538120dd8a4.jpg"]];    
 [imageView imageOptimizePerformance:CGSizeMake(20, 20) cornerPosition:UIRectCornerTopLeft|UIRectCornerTopRight];
[self.view addSubview:imageView];

如果需要更好的性能,加上蒙版加上绘制,但是以这种方式已经可以达到微信朋友圈的性能要求了,当然你如果想要微博一样的性能,就需要对cell的加载进行大量的优化了。

上一篇 下一篇

猜你喜欢

热点阅读