iOS UIImageView渲染

来源:互联网 发布:centos显卡驱动安装 编辑:程序博客网 时间:2024/06/15 21:55

在UITaleView加载时总会滑动卡顿时原因之一就是图片的渲染,所以我就对UIImageView进行研究,我们以前的思路是根据贝塞尔曲线对图片进行处理

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

我发现一些UIImageView不常用的属性也可以缓解UIImageView渲染问题。

shouldRasterize

当shouldRasterize设成true时,layer被渲染成一个bitmap,并缓存起来,等下次使用时不会再重新去渲染了。实现圆角本身就是在做颜色混合(blending),如果每次页面出来时都blending,消耗太大,这时shouldRasterize = yes,下次就只是简单的从渲染引擎的cache里读取那张bitmap,节约系统资源。
我们在滚动tableView时,如果每次都执行圆角设置,肯定会阻塞卡顿,设置这个将会使滑动更加流畅。

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

别忘了设置适当的rasterizationScale,否则在retina的设备上这些视图会成锯齿状。

0 0