UIImageView简单的性能优化

看代码吧~

  • 可以设置上下左右四个方向任意圆角
  • 更具图片大小判断圆角大小
分类: 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的加载进行大量的优化了。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 13,887评论 1 32
  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,054评论 3 119
  • 1.ios高性能编程 (1).内层 最小的内层平均值和峰值(2).耗电量 高效的算法和数据结构(3).初始化时...
    欧辰_OSR阅读 30,001评论 8 265
  • 2018年已经过去14天,330+个小时,我这才开始写我每年一定会做的年度碎碎念。一年的时间总是越来越短,好像在2...
    一大只羊阅读 3,207评论 1 1
  • 虽然创业的起点在于痛点的发现,创始人拉上一帮人成立团队要开干后,关于公司注册的问题马上就提上议程了,公司注册看起来...
    wleven阅读 2,532评论 0 4

友情链接更多精彩内容