zoukankan      html  css  js  c++  java
  • [iOS]UIImageView增加圆角

    [iOS]UIImageView增加圆角

    "如何给一个UIImageView增加圆角?有几种方法?各自区别?"

    备注:本文参考自http://www.jianshu.com/p/d1954c9a4426

    UIImageView *poImgView = [[UIImageView alloc]init];

    方案A(基本方案):

    poImgView.layer.cornerRadius = poImgView.frame.size.width/2.0;
    
    poImgView.layer.masksToBounds = YES;
    (或者 poImgView.clipsToBounds = YES;)

    备注:clipsToBounds是对view的切割,masksToBounds是对layer的切割

    性能消耗:
    这个是离屏渲染(off-screen-rendering),对性能消耗比较大
    fps大致在45帧左右(每个cell 做2个imageview)

    正常fps是60帧,越小,用户体验越差

    |

    离屏渲染,指的是GPU在当前屏幕缓冲区以外新开辟一个缓冲区进行渲染操作。由上面的一个结论视图和圆角的大小对帧率并没有什么卵影响,数量才是伤害的核心输出啊。可以知道离屏渲染耗时是发生在离屏这个动作上面,而不是渲染。为什么离屏这么耗时?原因主要有创建缓冲区和上下文切换。创建新的缓冲区代价都不算大,付出最大代价的是上下文切换。

    方案B:

    CAShapeLayer *layer = [CAShapeLayer layer];  
    UIBezierPath *aPath = [UIBezierPath bezierPathWithOvalInRect:aImageView.bounds];  
    layer.path = aPath.CGPath;  
    poImgView.layer.mask = layer;

    性能消耗:
    测试fps大致在20帧左右,比方案A的消耗更大

    方案C:

    - (UIImage *)imageWithCornerRadius:(CGFloat)radius {
    CGRect rect = (CGRect){0.f, 0.f, self.size};
    
    UIGraphicsBeginImageContextWithOptions(self.size, NO, UIScreen.mainScreen.scale);
    CGContextAddPath(UIGraphicsGetCurrentContext(),
     [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius].CGPath);
    CGContextClip(UIGraphicsGetCurrentContext());
    
    [self drawInRect:rect];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return image;
    }

    性能消耗:
    这个是on-screen-rendering
    相当于时时去做渲染,相比于A.B方案的离线渲染,此方法对性能消耗最低,推荐用此方案。

    说明

    方案A和方案B都是比较常见的方式,但是都不推荐,对此的优化方案是是用C,但是如果费用使用A或者B方案,补救措施是:

      1.栅格化:将cell的所有内容生成一张独立的图像,在屏幕滚动时,只显示图像;这将隐式的创建一个位图,各种阴影遮罩等效果也会保存到位图中并缓存起来,从而减少渲染的频度(不是矢量图);这个命令会让视图渲染的内容被缓存下来,下一次绘制的时候直接显示缓存。

      self.layer.shouldRasterize = YES;

      2.指定分辨率,否则默认使用 *1 的图像,需要指定和屏幕相匹配的分辨率

      self.layer.rasterizationScale = [UIScreen mainScreen].scale;
  • 相关阅读:
    Hanoi塔
    采药
    进制转换(大数)
    Load Balancing with NGINX 负载均衡算法
    upstream模块实现反向代理的功能
    epoll
    在nginx启动后,如果我们要操作nginx,要怎么做呢 别增加无谓的上下文切换 异步非阻塞的方式来处理请求 worker的个数为cpu的核数 红黑树
    粘性会话 session affinity sticky session requests from the same client to be passed to the same server in a group of servers
    负载均衡 4层协议 7层协议
    A Secure Cookie Protocol 安全cookie协议 配置服务器Cookie
  • 原文地址:https://www.cnblogs.com/ansyxpf/p/5655627.html
Copyright © 2011-2022 走看看