iOS設(shè)置圓角的4種方法實例(附性能評測)
四種設(shè)置圓角的方法
從網(wǎng)上收集了各種設(shè)置圓角的方法,總結(jié)起來有以下四種:
1、設(shè)置 layer 的 cornerRadius
view.layer.masksToBounds = YES; view.layer.cornerRadius = imgSize.width / 2;
2、用貝塞爾曲線作 mask 圓角
CAShapeLayer *layer = [CAShapeLayer layer]; UIBezierPath *aPath = [UIBezierPath bezierPathWithOvalInRect:view.bounds]; layer.path = aPath.CGPath; view.layer.mask = layer;
3、重新繪制圓角
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ UIImage *image = view.image; image = [image drawCircleImage]; dispatch_async(dispatch_get_main_queue(), ^{ view.image = image; }); }); //////////////////////// @implementation UIImage (RoundedCorner) - (UIImage *)drawCircleImage { CGFloat side = MIN(self.size.width, self.size.height); UIGraphicsBeginImageContextWithOptions(CGSizeMake(side, side), false, [UIScreen mainScreen].scale); CGContextAddPath(UIGraphicsGetCurrentContext(), [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, side, side)].CGPath); CGContextClip(UIGraphicsGetCurrentContext()); CGFloat marginX = -(self.size.width - side) / 2.f; CGFloat marginY = -(self.size.height - side) / 2.f; [self drawInRect:CGRectMake(marginX, marginY, self.size.width, self.size.height)]; CGContextDrawPath(UIGraphicsGetCurrentContext(), kCGPathFillStroke); UIImage *output = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return output; } @end
4、混合圖層,用一張鏤空的透明圖片作遮罩
cover@2x.png
UIView *parent = [view superview]; UIImageView *cover = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, imgSize.width, imgSize.height)]; cover.image = [UIImage imageNamed:@"cover"]; [parent addSubview:cover]; cover.center = view.center;
四種方法性能測試
網(wǎng)上流傳兩個結(jié)論:
- 第一種方法會引發(fā)離屏渲染,所以是最慢的;
- 據(jù)說第四種是效率最高的。
事實情況如何呢?
為了驗證網(wǎng)上的結(jié)論,需要找一種性能比較的方法,這里用 Instrument 的測試 FPS (幀數(shù))作為性能直觀的比較值,測試過程如下:
- 搭建collectionView 工程,連續(xù)刷新顯示 1萬個cell,每個cell使用相同圖片,排除不同照片帶來的差異;
- 在真機下運行分別運行四種方法,用 Instrument 記錄,并計算平均FPS;
- 為保證平均值準確,去掉頭尾幀數(shù)率過低的時間段。
1. 設(shè)置 layer 的 cornerRadius
2. 用貝塞爾曲線作 mask 圓角
3. 重新繪制圓角
4. 混合圖層,用一張鏤空的透明圖片作遮罩
結(jié)果排名如下
3 > 1 > 2 > 4
一點點優(yōu)化
第四種方式不但墊底,而且出奇的慢,說明我們的實現(xiàn)有明顯的問題,觀察代碼,發(fā)現(xiàn)原來的代碼沒有考慮 cell 復用,cove 視圖被反復添加到cell,哪有不慢的道理!!! 于是作以下優(yōu)化:
// 4. 混合圖層,用一張鏤空的透明圖片作遮罩 (優(yōu)化版) UIView *parent = [view superview]; if (![parent viewWithTag:13]) { UIImageView *cover = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, imgSize.width, imgSize.height)]; cover.image = [UIImage imageNamed:@"cover"]; cover.tag = 13; [parent addSubview:cover]; cover.center = view.center; }
這樣避免了View的重復添加,F(xiàn)PS 有了明顯的回升:
4.(優(yōu)化版)
優(yōu)化后的排名: 3 > 4 > 1 > 2
結(jié)論
測試的結(jié)論與網(wǎng)上流傳的幾乎完全不同,分析一下造成這種情況的原因:
- 蘋果在iOS9后優(yōu)化了 cornerRadius 的繪圖方式,方法1不再需要離屏渲染。
- 方法3基于單張位圖運算,方法2使用了矢量并與位圖疊加,導致運算量上升,觀察圖2的GPU運算量高達 80.2%,證明了這一推斷。
實際開發(fā)建議
- 方法1 設(shè)置簡單,性能差別不明顯,簡單圓角場景下推薦使用。
- 方法4 基于透明位圖,可用于異形遮罩,但需要根據(jù)圖片大小做多張?zhí)厥馕粓D,請根據(jù)實際情況選擇。
- 在位圖尺寸很大,數(shù)量很多的情況下,用方法3,但要注意內(nèi)存警告,最好配合緩存機制使用,避免因內(nèi)存溢出而崩潰。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
iOS開發(fā)之image圖片壓縮及壓縮成指定大小的兩種方法
這篇文章主要介紹了iOS開發(fā)之image圖片壓縮及壓縮成指定大小的兩種方法,需要的朋友可以參考下2017-11-11淺談iOS開發(fā)如何適配暗黑模式(Dark Mode)
這篇文章主要介紹了淺談iOS開發(fā)如何適配暗黑模式(Dark Mode),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09基于iOS Realm數(shù)據(jù)庫的使用實例詳解
下面小編就為大家分享一篇基于iOS Realm數(shù)據(jù)庫的使用實例詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01