繪制的主要的按鈕
1. Color Blended Layers
官方是這么描述它的:
Shows blended view layers. Multiple view layers that are drawn on top of each other with blending enabled are highlighted in red. Reducing the amount of red in your app when this option is selected can dramatically improve your app’s performance. Blended view layers often cause slow table scrolling.
簡單來說,屏幕上的每個(gè)像素點(diǎn)的顏色是由當(dāng)前像素點(diǎn)上的多層layer(如果存在)共同決定的奥吩,GPU會(huì)進(jìn)行計(jì)算出混合顏色的RGB值具伍,最終顯示在屏幕上。而這需要讓GPU計(jì)算圈驼,所以我們要盡量避免設(shè)置alpha人芽,這樣GPU會(huì)忽略下面所有的layer,節(jié)約計(jì)算量绩脆。
設(shè)置了透明的view會(huì)讓GPU計(jì)算圖層混合后的最終結(jié)果萤厅。
我想再提一下opaque這個(gè)屬性,網(wǎng)上普遍認(rèn)為view.opaque = YES靴迫,GPU就不會(huì)進(jìn)行圖層混合計(jì)算了惕味。而這個(gè)結(jié)論是錯(cuò)誤的,其實(shí)view.opaque事實(shí)上并沒什么卵用玉锌。
如果你真的想達(dá)到這個(gè)效果名挥,可以用layer.opaque,這個(gè)才是正確的做法。
關(guān)于Color Blended Layers有幾個(gè)注意點(diǎn)
- 如果label中的內(nèi)容包含中文主守,label實(shí)際渲染區(qū)域要大于label的size禀倔,最外層會(huì)多一個(gè)subLayer榄融,所以即使設(shè)置背景色不透明,仍然會(huì)發(fā)生混色救湖。因此愧杯,還需要添加masksToBounds = YES
- UIImageView不僅需要自身容器不透明,并且imageView包含的內(nèi)容圖片也必須是不透明的
- 如果使用的是CALayer鞋既,要把opaque屬性設(shè)置成YES(默認(rèn)是NO)力九。如果是用的UIView, opaque屬性默認(rèn)是YES。
綠色越多邑闺,代表混合的情況越少跌前,紅色越多App UI急需改進(jìn).
(一句話概括盡量不要有紅色區(qū)域)
2. Color Copied Images
官方是這么描述它的:
If an image is in a color format that the GPU can not directly work with, it will be converted in the CPU.
簡而言之,蘋果的GPU只解析32bit的顏色格式陡舅,記住是32bit舒萎。
如果你放一張圖片,而它的顏色格式卻不是32bit蹭沛,CPU會(huì)先進(jìn)行顏色格式轉(zhuǎn)換臂寝,再讓GPU渲染。乖乖的CPU就默默做了這個(gè)多余的工作摊灭。
所以給你兩個(gè)選擇:
- (1) 讓設(shè)計(jì)濕都給你切32bit的圖
- (2) 自己去跑個(gè)異步線程來轉(zhuǎn)換顏色去吧咆贬,不要去堵塞本來就壓力很大的主線程!
你選哪個(gè)帚呼?當(dāng)然是讓設(shè)計(jì)濕切圖啦掏缎,我才不愿意多寫代碼。
而且于情于理煤杀,就算異步轉(zhuǎn)換顏色眷蜈,也會(huì)導(dǎo)致性能損耗,比如電量增多沈自,發(fā)熱強(qiáng)變大等等等等酌儒。
“If an image is in a color format that the GPU can not directly work with,it will be converted in the CPU.”
Session 419 WWDC 2014中詳細(xì)介紹了Color Copied Images.
蘋果的GPU只解析32bit的顏色格式。
如果一張圖片枯途,顏色格式不是32bit忌怎,CPU會(huì)先進(jìn)行顏色格式轉(zhuǎn)換,再讓GPU渲染酪夷。
就算異步轉(zhuǎn)換顏色榴啸,也會(huì)導(dǎo)致性能損耗,比如電量增多晚岭,發(fā)熱強(qiáng)變大等等鸥印。
解決辦法:讓設(shè)計(jì)師提供32bit顏色格式的圖片。
3. Color Misaligned Images
官方是這么描述它的:
Misaligned Image表示要繪制的點(diǎn)無法直接映射到頻幕上的像素點(diǎn),此時(shí)系統(tǒng)需要對相鄰的像素點(diǎn)做anti-aliasing反鋸齒計(jì)算库说,增加了圖形負(fù)擔(dān)狂鞋,通常這種問題出在對某些View的Frame重新計(jì)算和設(shè)置時(shí)產(chǎn)生的。
很簡單璃弄,不要出現(xiàn)image size與imageView size不同的情況要销,這樣會(huì)觸發(fā)反鋸齒計(jì)算构回,增加性能損耗夏块。
所以,實(shí)際開發(fā)中纤掸,本地的圖片比較好把控脐供,只需要寫好對應(yīng)的尺寸就好了,但是對于download下來的圖片借跪,可以在加載完后進(jìn)行size處理政己,以滿足imageView frame。特別是對于很多app掏愁,有大量的tableview歇由,如果進(jìn)行處理,則會(huì)大幅度提高流暢度果港。
http://www.reibang.com/p/38cf9c170141 比較好的 資料
4. Off-Screen Rendering 離屏渲染
http://www.reibang.com/p/f247f8c13b32
GPU屏幕渲染有兩種方式
On-Screen Rendering 當(dāng)前屏幕渲染沦泌,是指渲染操作是在當(dāng)前用于顯示屏幕緩沖區(qū)中進(jìn)行的。
Off-Screen Rendering 離屏渲染辛掠,指的是GPU在當(dāng)前屏幕緩沖區(qū)以外新開辟一個(gè)緩沖區(qū)進(jìn)行渲染操作谢谦。
另一種特殊的離屏渲染:CPU渲染
如果我們重寫了drawRect方法,并且使用任何Core Graphics的技術(shù)進(jìn)行了繪制操作萝衩,就涉及到了CPU渲染回挽。整個(gè)渲染過程由CPU在App內(nèi)同步地完成,渲染得到的bitmap最后再交由GPU用于顯示猩谊。
相比于當(dāng)前屏幕渲染千劈,離屏渲染的代價(jià)比較高,主要體現(xiàn)在兩個(gè)方面:
創(chuàng)建新緩沖區(qū)
想要離屏渲染牌捷,就必須創(chuàng)建一個(gè)新的緩沖區(qū)队塘;
上下文切換
離屏渲染的整個(gè)過程,需要多次切換上下文環(huán)境:先是從當(dāng)前屏幕(On-Screen)切換到離屏(Off-Screen)宜鸯;等到離屏渲染結(jié)束以后憔古,將離屏緩沖區(qū)的渲染結(jié)果顯示到屏幕上。而上下文環(huán)境的切換是要付出很大代價(jià)的淋袖。
http://www.reibang.com/p/f247f8c13b32
5. 相關(guān)的英文文檔
1:Core Animation Dubug Options (官方文檔)
Core Animation contains a number of useful debugging options in the display settings area of the inspector pane. You do not need to be running a trace to use these options on your iOS device.1.1 :Color Blended Layers.(一句話概括盡量不要有紅色區(qū)域)
Shows blended view layers. Multiple view layers that are drawn on top of each other with blending enabled are highlighted in red. Reducing the amount of red in your app when this option is selected can dramatically improve your app’s performance. Blended view layers often cause slow table scrolling.
- 1.2 :Color Hits Green and Misses Red
Marks views in green or red. A view that is able to use a cached rasterization is marked in green 綠色.
- 1.3 :Color Copied Images
Shows images that are copied by Core Animation in blue 藍(lán)色.
- 1.4 :Color Immediately
Removes the 10 ms delay when performing color-flush operations.
- 1.5 :Color Misaligned Images
Places a magenta 品紅 overlay over images where the source pixels are not aligned to the destination pixels.
- 1.6 :Color Offscreen-Rendered Yellow
Places a yellow 黃色 overlay over content that is rendered offscreen.
- 1.7 :Color OpenGL Fast Path Blue
Places a blue overlay over content that is detached from the compositor.
6. 相關(guān)的資料的集合
iOS APP渲染性能優(yōu)化http://www.reibang.com/p/c9cdf044487b
離屏渲染(OffScreen Rendering)http://www.reibang.com/p/505d4a644c20
iOS 離屏渲染優(yōu)化(Offscreen Render)
http://www.reibang.com/p/505d4a644c20
關(guān)于iOS的離屏渲染的分析
https://juejin.cn/post/6873761559936499725
淺談iOS中的視圖優(yōu)化
http://www.reibang.com/p/5d0aa1140e72
iOS Color Misaligned Images優(yōu)化
http://www.reibang.com/p/38cf9c170141
iOS中的視圖優(yōu)化-Color Copied Images
https://blog.csdn.net/Nathan1987_/article/details/78760939
iOS-Color Blended Layers
http://www.reibang.com/p/bdd39a56142a
instrument 之Core-Animation 性能調(diào)優(yōu)(Color Blended Layers)
http://www.reibang.com/p/5cdbda3a0f56
iOS 開發(fā):繪制像素到屏幕https://segmentfault.com/a/1190000000390012
iOS 使用Core Animation檢測圖形性能與相關(guān)優(yōu)化
https://www.codenong.com/jsba12cfb64e96/
UIKit 性能優(yōu)化(圖層混合鸿市、光柵化、顏色格式、圖片大小焰情、離屏渲染)
https://www.cnblogs.com/sunyanyan/p/5292257.html
影響動(dòng)畫性能的因素及如何使用 Instruments 檢測.md
https://github.com/pro648/tips/blob/master/sources/%E5%BD%B1%E5%93%8D%E5%8A%A8%E7%94%BB%E6%80%A7%E8%83%BD%E7%9A%84%E5%9B%A0%E7%B4%A0%E5%8F%8A%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20Instruments%20%E6%A3%80%E6%B5%8B.md