iOS高效裁剪圓角

平時代碼中用cornerRadius和masksToBounds兩個屬性裁出圓角,這樣雖然簡單,但會由于離屏渲染(Off-Screen Rendering),會產(chǎn)生性能問題。一篇對離屏渲染研究的文章http://www.itdecent.cn/p/6d24a4c29e18

高效裁剪的方法是:切換到工作線程利用CoreGraphic API生成一個offscreen UIImage,再切換到main thread賦值給UIImageView。

- (void)cornerRadiusWithImage:(UIImage *)image imageView:(UIImageView *)imageView UIColor:(UIColor *)color{
    CGSize size = imageView.bounds.size;
    CGFloat scale = [UIScreen mainScreen].scale;

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        /**
         * 創(chuàng)建一個基于位圖的上下文(context),并將其設(shè)置為當(dāng)前上下文(context)
         * size——同UIGraphicsBeginImageContext
         * opaque—透明開關(guān),如果圖形完全不用透明,設(shè)置為YES以優(yōu)化位圖的存儲。
         * scale—–縮放因子
         */
        UIGraphicsBeginImageContextWithOptions(size, YES, scale);
        
        CGContextRef currentContext = UIGraphicsGetCurrentContext();
        
        if (nil == currentContext) {
            return;
        }
        UIBezierPath *beziPath = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.bounds.size.height/2];
        
        UIBezierPath *backgroundRect = [UIBezierPath bezierPathWithRect:imageView.bounds];
        [color setFill];
        [backgroundRect fill];

        //簡單的說,就是一個path調(diào)用addClip之后,它所在的context的可見區(qū)域就變成了它的“fill area”,接下來的繪制,如果在這個區(qū)域外都會被無視。
        [beziPath addClip];
        [image drawInRect:imageView.bounds];
        UIImage *processedImageRef = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        dispatch_async(dispatch_get_main_queue(), ^{
            imageView.image = processedImageRef;
        });
        
    });
    
}
?著作權(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)容