iOS 滑动性能优化

2018-03-16  本文已影响0人  无声无息泡泡糖

转自[一片枫叶]

一、 减少图层的Blend操作

1.1: 图层的blend的alpha的叠加

展示半透明的view,设备会把当前图层和背景图层进行alpha叠加,这是一项很耗性能的一件事。如果动画中每一帧都做叠加,性能的损耗是很严重。

Disable alpha blending except where needed. Unless you are intentionally working with images that contain transparency (drawing UI elements, for example), you should generally mark the view as opaque by checking Opaque checkbox in the attributes inspector, or setting the opaque property on the view itself.
1.2: UIImageView的半透明的处理

UIImageView的半透明取决于以下几项:

An opaque view is expected to fill its bounds with entirely opaque content—that is, the content should have an alpha value of 1.0. If the view is opaque and either does not fill its bounds or contains wholly or partially transparent content, the results are unpredictable. You should always set the value of this property to NO if the view is fully or partially transparent.
1.3: 图层的Blend操作规则
1.4: 图层的Blend总结

适用场景: 通用优化规则,不会造成副作用

二、适当使用Rasterize

针对内容比较固定的Cell,建议采用光栅化,让Core Animation框架帮我们完成图层的混合,生成一个静态图,优化帧率。

适用场景
UITableView & UICollectionView & UIScrollView中内容变化不频繁的Cell

注:此优化需要Profile,使用Core Animation工具中的ColorHitsGreenandMissesRed工具调优
如果使用不当,可能适得其反

三、避免图片资源的重采样

Image views can perform two operations that are relatively expensive performance-wise: scaling the image and alpha compositing the image with lower layers.

减少图片资源的重采样是一个费时给力的过程,涉及到插值算法,以双线性插值为例,每插值一个点需要用到周围四个点的像素值,运算量可见一斑。

直接对于UIImageView设置一个大图,在实际展示的时候会在主线程完成重采样的过程,耗时耗内存。

如何避免?

适用场景
所有需要使用图片的场景都可以使用此方案优化,无副作用。

四: 总结

滑动性能优化这块儿涉及到的知识还是挺多的,不要盲目,过早的优化。使用Instrument找出瓶颈,然后合理使用不同的方案。性能优化有很多奇淫技巧,但通常做到上面几个大的点,基本上性能就能接受了。

对于TableView & Collection View这块儿还有一个很有效的优化手段,在快速滑动的时候,忽略中间快速闪过的Cell,直接借用UIScrollView的delegate判断加载滑动停止目标区域的Cell的内容。实践证明此方法效果还是很明显的,具体例子可以网上搜一下。

iOS Core Animation: Advanced Techniques
UIImageView Class Reference
Image Resizing Techniques
iOS 保持界面流畅的技巧
Alpha compositing

上一篇 下一篇

猜你喜欢

热点阅读