先看效果圖 ↓

方法一:兩張圖片疊出邊框
用一張背景圖當外圈、一張前景圖放中間,再對兩個 imageView 設定 cornerRadius 做圓角:
backImageView.layer.cornerRadius = backImageView.frame.size.width / 2; backImageView.layer.masksToBounds = YES; frontImageView.layer.cornerRadius = frontImageView.frame.size.width / 2; frontImageView.layer.masksToBounds = YES;
顯然這不是今天的主角。這種做法需要兩張圖,而接下來的方法只要一張圖就能產出帶邊框的圓形頭像。
方法二:用圖形上下文直接畫出帶邊框的圓形圖片
// borderWidth 為邊框寬度
CGFloat imageW = image.size.width + 2 * borderWidth;
CGFloat imageH = imageW;
CGSize imageSize = CGSizeMake(imageW, imageH);
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
// borderColor 為邊框顏色
[borderColor set];
CGFloat bigRadius = imageW * 0.5;
CGFloat centerX = bigRadius;
CGFloat centerY = bigRadius;
CGContextAddArc(context, centerX, centerY, bigRadius, 0, M_PI * 2, 0);
CGContextFillPath(context);
CGFloat smallRadius = bigRadius - borderWidth;
CGContextAddArc(context, centerX, centerY, smallRadius, 0, M_PI * 2, 0);
CGContextClip(context);
[image drawInRect:CGRectMake(borderWidth, borderWidth,
image.frame.size.width, image.frame.size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
程式碼逐段解讀
1. 原理:先畫一個寬度為 borderWidth 的圓環,再把圖片放進圓環內,只保留圓環內的部分。
2. 前三行:算出帶邊框後的整體寬高(要正圓,寬高必須相同),並準備一個 CGSize 給下一步用。
3. 開啟圖形上下文:
UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale)
第一個參數是繪圖範圍,也就是剛才的 imageSize;第二個參數是否不透明,填 NO 即可(若為 YES,alpha 通道會被忽略,整張圖視為完全不透明);第三個參數 scale 是縮放比例,填 0.0 代表採用主螢幕的比例,讓成品在 Retina 螢幕上依然清晰。
4. 取得目前上下文:
CGContextRef context = UIGraphicsGetCurrentContext();
5. 設定顏色:
[borderColor set];
這行的作用是「設定目前繪圖上下文的填色與描邊色」,相當於先把顏料準備好,等一下替圓環上色。
6. 畫外圓:
CGFloat bigRadius = imageW * 0.5; CGFloat centerX = bigRadius; CGFloat centerY = bigRadius; CGContextAddArc(context, centerX, centerY, bigRadius, 0, M_PI * 2, 0);
畫圓需要圓心與半徑。bigRadius 是含邊框整體的半徑,centerX/centerY 是圓心座標。CGContextAddArc 的完整簽名如下:
void CGContextAddArc(CGContextRef cg_nullable c, CGFloat x, CGFloat y,
CGFloat radius, CGFloat startAngle, CGFloat endAngle,
int clockwise)
參數意義:c 為上下文;x、y 為圓心座標;radius 為半徑;startAngle 為起始角;endAngle 為結束角(M_PI * 2 表示一整圈);clockwise 順時針填 1、逆時針填 0。
7. 填色:
CGContextFillPath(context);
用剛才設定的填色把路徑填滿,此時上下文裡是一個半徑 bigRadius 的實心圓。
8. 再畫內圓:
CGFloat smallRadius = bigRadius - borderWidth; CGContextAddArc(context, centerX, centerY, smallRadius, 0, M_PI * 2, 0);
smallRadius 就是中間照片的半徑,也就是扣掉邊框寬度後剩下的部分。
9. 設定裁剪區域:
CGContextClip(context);
官方文件的說法是「將目前路徑與剪貼路徑相交,並以結果作為後續繪圖的剪貼範圍」。白話就是:只保留內圓裡的區域,後續畫上去的東西不會超出這個圓。
10. 把圖片畫進去:
[image drawInRect:CGRectMake(borderWidth, borderWidth,
image.frame.size.width, image.frame.size.height)];
圖片會被畫進指定矩形,而且只保留裁剪路徑內的部分。此時上下文已經變成一張帶邊框的圓形圖了。
11. 取出成品並收拾善後:
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
記得呼叫 UIGraphicsEndImageContext 關閉上下文,否則在大量產生圖片的情境下會累積記憶體。
兩種方法的差異
方法一靠圖層疊加,在視覺上「看起來」有邊框;方法二則是真正產出一張帶邊框的圓形圖片。前者程式碼短,後者較複雜但更靈活,邊框寬度、顏色都能任意調整,也方便快取後重複使用。
結語
日常開發中要依需求選擇實作方式。如果只是畫面上要圓形頭像,方法一足矣;如果需要一張可以存檔、可以傳給別人的圓形圖,就用方法二。