iOS開發(fā)中使用layer.cornerRadius圓角影響性能的解決辦法

在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è)置這個將會使滑動更加流暢。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容