• iOS开发-UIImageView高效设置Radius


    圆角的设置在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

  • 相关阅读:
    hibernate 注解text,大文本类型
    ARRAYLIST VECTOR LINKEDLIST 区别与用法(转载)
    Javascript 异步加载详解
    浏览器是怎样工作的(二):渲染引擎,HTML解析
    浏览器是怎样工作的(一):基础知识
    ajax和json
    30+ CSS Grid System
    写好高效CSS的定律
    960网页栅格化总结
    响应式网站之测试工具
  • 原文地址:https://www.cnblogs.com/xiaofeixiang/p/5123169.html
Copyright © 2020-2023  润新知