在iOS中,對UIView或UIimageVIew使用layer.cornerRadius圓角設(shè)置時,會很耗性能,也會影響UI流暢,造成App很卡,當(dāng)關(guān)閉了圓角后,重新運行就會流暢多了。但是產(chǎn)品的需求必須加圓角,沒辦法,去stackoverflow找方案,發(fā)現(xiàn)方法都大同小異,只不過是繪制上做一些優(yōu)化。后來查看layer的頭文件,最后找到了一個牛B的屬性
[cpp] view plaincopy
/* 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;
view.layer.shouldRasterize = YES(光柵化)的使用
當(dāng)shouldRasterize設(shè)成true時,layer被渲染成一個bitmap,并緩存起來,等下次使用時不會再重新去渲染了。實現(xiàn)圓角本身就是在做顏色混合(blending),如果每次頁面出來時都blending,消耗太大,這時shouldRasterize = yes,下次就只是簡單的從渲染引擎的cache里讀取那張bitmap,節(jié)約系統(tǒng)資源。
額外收獲:如果在滾動tableView時,每次都執(zhí)行圓角設(shè)置,肯定會阻塞UI,設(shè)置這個將會使滑動更加流暢。