美文网首页
ios开发shouldRasterize光栅化--离屏渲染

ios开发shouldRasterize光栅化--离屏渲染

作者: lltree | 来源:发表于2018-12-11 17:11 被阅读13次

shouldRasterize(光栅化)是比较特别的一种离屏渲染:
光栅化概念:将图转化为一个个栅格组成的图象。
光栅化特点:每个元素对应帧缓冲区中的一像素。

/* When true, the layer is rendered as a bitmap in its local coordinate
 * space ("rasterized"), then the bitmap is composited into the
 * destination (with the minificationFilter and magnificationFilter
 * properties of the layer applied if the bitmap needs scaling).
 * Rasterization occurs after the layer's filters and shadow effects
 * are applied, but before the opacity modulation. As an implementation
 * detail the rendering engine may attempt to cache and reuse the
 * bitmap from one frame to the next. (Whether it does or not will have
 * no affect on the rendered output.)
 *
 * When false the layer is composited directly into the destination
 * whenever possible (however, certain features of the compositing
 * model may force rasterization, e.g. adding filters).
 *
 * Defaults to NO. Animatable. */

@property BOOL shouldRasterize;

/* The scale at which the layer will be rasterized (when the
 * shouldRasterize property has been set to YES) relative to the
 * coordinate space of the layer. Defaults to one. Animatable. */

@property CGFloat rasterizationScale;

光栅化有什么好处?

shouldRasterize设成true时,layer被渲染成一个bitmap位图,并缓存起来,等下次使用时不会再重新去渲染了。实现圆角本身就是在做颜色混合(blending),如果每次页面出来时都blending,消耗太大,这时shouldRasterize = yes,下次就只是简单的从渲染引擎的cache里读取那张bitmap,节约系统资源。

而光栅化会导致离屏渲染,影响图像性能,那么光栅化是否有助于优化性能,就取决于光栅化创建的位图缓存是否被有效复用,而减少渲染的频度。可以使用Instruments进行检测:

当你使用光栅化时,你可以开启“Color Hits Green and Misses Red”来检查该场景下光栅化操作是否是一个好的选择。
如果光栅化的图层是绿色,就表示这些缓存被复用;如果是红色就表示缓存会被重复创建,这就表示该处存在性能问题了。

注意:
对于经常变动的内容,这个时候不要开启,否则会造成性能的浪费

示例

光栅化是将一个layer预先渲染成位图(bitmap),然后加入缓存中。如果对于阴影效果这样比较消耗资源的静态内容进行缓存,可以得到一定幅度的性能提升。demo中的这一行代码表示将label的layer光栅化:

label.layer.shouldRasterize = YES;

注意:光栅化的核心在于缓存的思想。我们自己动手把玩一下,可以发现以下几个有意思的现象:

1 上下微小幅度滑动时,一直是绿色
2.上下较大幅度滑动,新出现的label一开始是红色,随后变成绿色
3.如果静止一秒钟,刚开始滑动时会变红。

这是因为layer进行光栅化后渲染成位图放在缓存中。当屏幕出现滑动时,我们直接从缓存中读取而不必渲染,所以会看到绿色。当新的label出现时,缓存中没有个这个label的位图,所以会变成红色。第三点比较关键,缓存中的对象有效期只有100ms,即如果在0.1s内没有被使用就会自动从缓存中清理出去。这就是为什么停留一会儿再滑动就会看到红色。

光栅化的缓存机制是一把双刃剑,先写入缓存再读取有可能消耗较多的时间。因此光栅化仅适用于较复杂的、静态的效果。通过Instrument的调试发现,这里使用光栅化经常出现未命中缓存的情况,如果没有特殊需要则可以关闭光栅化,所以我们做的第二个优化是注释掉下面这行代码

//label.layer.shouldRasterize = YES;

原文,此处只为学习

相关文章

  • iOS性能优化涉及方面总结

    性能优化:界面卡顿优化:1、减少离屏渲染,造成离屏渲染的有shouldRasterize(光栅化)、mask(遮罩...

  • ios开发shouldRasterize光栅化--离屏渲染

    shouldRasterize(光栅化)是比较特别的一种离屏渲染:光栅化概念:将图转化为一个个栅格组成的图象。光栅...

  • 离屏渲染

    哪些操作会触发离屏渲染? 1、光栅化,layer.shouldRasterize = YES2、遮罩,layer....

  • iOS shouldRasterize光栅化--离屏渲染

    参考资料:ios开发中view.layer.shouldRasterize = YES 的使用说明iOS 离屏渲染...

  • 哪些场景可以触发离屏渲染?

    设置了以下属性时,都会触发离屏绘制: shouldRasterize(光栅化)masks(遮罩)shadows(阴...

  • 性能优化之-tableView的栅格化(光栅化)

    关于CALayer的shouldRasterize(光栅化) 开启shouldRasterize后,CALayer...

  • 零零碎碎的知识点

    1.圆角,图层遮罩,阴影或者是图层光栅化(GPU离屏渲染)会强制Core Animation提前渲染图层的离屏绘制...

  • 性能优化的点

    1 离屏渲染 光栅化 圆角(生成圆角图 系统圆角 贝塞尔曲线遮罩) 后两个会离屏渲染 阴影 加上showdowPa...

  • iOS笔记

    iOS 性能优化 产生离屏渲染:圆角,阴影,光栅化,透明度,遮罩, 使用CGContext在drawRect中绘制...

  • iOS离屏渲染的检测和优化

    在iOS离屏渲染介绍了离屏渲染的逻辑和原理,我们知道离屏渲染对于性能会有较大的消耗,那么开发中怎么避免产生离屏渲染...

网友评论

      本文标题:ios开发shouldRasterize光栅化--离屏渲染

      本文链接:https://www.haomeiwen.com/subject/vxeehqtx.html