UIView self.layer.shouldRasterize = YES 和性能问题 [英] UIView self.layer.shouldRasterize = YES and performance issues

查看:20
本文介绍了UIView self.layer.shouldRasterize = YES 和性能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想分享我在 UIViews 上使用 self.layer.shouldRasterize = YES; 标志的经验.

我有一个 UIView 类层次结构,为了提高滚动性能(all其中的静态子视图大于设备的屏幕).

今天我在其中一个子类中使用了 CAEmitterLayer 来产生漂亮的粒子效果.

性能真的很差,虽然粒子的数量确实很少(50 个粒子).

这个问题的原因是什么?

解决方案

我只是引用Apple Doc并解释一下:

@property BOOL 应该光栅化

<块引用>

当该属性值为YES时,图层为在其局部坐标空间中呈现为位图,然后合成与任何其他内容一起到达目的地.阴影效果和任何过滤器属性中的过滤器被光栅化并包含在位图.但是,图层的当前不透明度未栅格化.如果光栅化的位图在合成过程中需要缩放,则minificationFilter 和 magnificationFilter 属性中的过滤器根据需要应用.

所以基本上当 shouldRasterize 设置为 YES 时,将计算构成图层的每个像素,并将整个图层缓存为位图.

  • 您何时能从中受益?

当您只需要绘制一次时.这意味着当您只需要纯粹的简单"动画(例如移动、变换、缩放...)时,因为 CoreAnimation 实际上会使用该层,而无需在每一帧都重新绘制它.结合 CoreAnimation 缓存复杂图层(带有阴影和圆角半径)是一项非常强大的功能.

  • 它什么时候会杀死你的帧率?

当您的图层被重新显示多次时,因为在已经生效的绘图之上,shouldRasterize 将处理所有像素以缓存位图数据.

所以你应该问自己的真正问题是:我在哪一层上应用 shouldRasterize 到 YES ?以及该层多久重绘一次?"

希望这已经足够清楚了.

I would like to share my experience from using self.layer.shouldRasterize = YES; flag on UIViews.

I have a UIView class hierarchy that has self.layer.shouldRasterize turned ON in order to improve scrolling performance (all of them have STATIC subviews that are larger than the screen of the device).

Today in one of the subclasses I used CAEmitterLayer to produce nice particle effects.

The performance is really poor although the number of particles was really low (50 particles).

What is the cause of this problem?

解决方案

I'll just quote Apple Doc and explain:

@property BOOL shouldRasterize

When the value of this property is YES, the layer is rendered as a bitmap in its local coordinate space and then composited to the destination with any other content. Shadow effects and any filters in the filters property are rasterized and included in the bitmap. However, the current opacity of the layer is not rasterized. If the rasterized bitmap requires scaling during compositing, the filters in the minificationFilter and magnificationFilter properties are applied as needed.

So basically when shouldRasterize is set to YES, every pixel that will compose the layer is calculated and the whole layer is cached as a bitmap.

  • When will you benefit from it ?

When you only need to draw it once. That means when you need just pure "simple" animation (eg moving, transform, scaling...) because CoreAnimation will actually use that layer without redrawing it every frame. It's a very powerful feature to cache complex layers (with shadows and corner radius) combined with CoreAnimation.

  • When will it kill you framerate ?

When your layer is redisplayed many times, because on top of the drawing that is already taking effect, the shouldRasterize will process all pixels to cache the bitmap data.

So the real question you should ask yourself is this : "On which layer am I applying the shouldRasterize to YES ? And how often is this layer redrawn ?"

Hope this was clear enough.

这篇关于UIView self.layer.shouldRasterize = YES 和性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆