博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS开发-UIImageView高效设置Radius
阅读量:7241 次
发布时间:2019-06-29

本文共 1740 字,大约阅读时间需要 5 分钟。

圆角的设置在iOS中随处可见,开发的时候也很方便,但是有的时候如果一个页面有大量的需要设置圆角的图片,容易产生性能问题,UIImageView ios9.0之前设置圆角是会产生离屏渲染的,9.0之后不会产生离屏渲染

因此需要日常设置圆角的方法上加一些改动:

1.最简单的图片圆角设置:

self.imageView=[[UIImageView alloc]initWithFrame:CGRectMake(100,200, 100, 100)];    [self.imageView setImage:[UIImage imageNamed:@"FlyElephant.jpg"]];    self.imageView.layer.cornerRadius=50;    self.imageView.layer.masksToBounds=YES;    [self.view addSubview:self.imageView];

2.设置Rasterize栅格化处理,会将图片放在缓存区,不会不断的进行图片渲染:

self.imageView=[[UIImageView alloc]initWithFrame:CGRectMake(100,200, 100, 100)];    [self.imageView setImage:[UIImage imageNamed:@"dress3.jpg"]];    self.imageView.layer.cornerRadius=50;    self.imageView.layer.shouldRasterize = YES;    self.imageView.clipsToBounds=YES;    self.imageView.layer.rasterizationScale=[UIScreen mainScreen].scale;  //不设置会模糊,不相信可以自己尝试    [self.view addSubview:self.imageView];

3.UIBezierPath贝塞尔曲线绘制(推荐)

self.imageView=[[UIImageView alloc]initWithFrame:CGRectMake(100,200, 100, 100)];    UIImage *anotherImage = [UIImage imageNamed:@"FlyElephant.jpg"];    //注意第三个选项的设置    UIGraphicsBeginImageContextWithOptions(self.imageView.bounds.size, NO, [UIScreen mainScreen].scale);    //在绘制之前先裁剪出一个圆形    [[UIBezierPath bezierPathWithRoundedRect:self.imageView.bounds                                cornerRadius:50] addClip];    //图片在设置的圆形里面进行绘制    [anotherImage drawInRect:self.imageView.bounds];    //获取图片    self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();    //结束绘制    UIGraphicsEndImageContext();    [self.view addSubview:self.imageView];

参考资料:http://stackoverflow.com/questions/11049016/cliptobounds-and-maskstobounds-performance-issue

http://stackoverflow.com/questions/17593524/using-cornerradius-on-a-uiimageview-in-a-uitableviewcell

转载地址:http://ksybm.baihongyu.com/

你可能感兴趣的文章
我的友情链接
查看>>
Soap和REST
查看>>
数据结构中各种排序的思路
查看>>
配置服务主机名和域名IP解析
查看>>
5种I/O模型
查看>>
3月23日作业
查看>>
RAID
查看>>
0326作业
查看>>
apache bench测试
查看>>
linux 命令 ls
查看>>
Java性能优化技巧集锦
查看>>
Hadoop 新 MapReduce 框架 Yarn 详解
查看>>
HP C7000刀片服务器实战2:RAID1创建
查看>>
Linux内核参数和系统连接数的优化
查看>>
Split和substring用法
查看>>
路由器配置Stub后 邻接关系建立失败
查看>>
【Linux基础】Linux文件系统
查看>>
spring整合任务调度
查看>>
hydra 暴力破解
查看>>
酷点2.0
查看>>