美文网首页
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开发shouldRasterize光栅化--离屏渲染

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