當(dāng)我們?cè)谧鯝PP的個(gè)人中心的時(shí)候,總會(huì)有用戶(hù)頭像的需求,用戶(hù)頭像最好是圓形的,我們有一下三種方式將一種圖片切成圓形:
第一種方法:通過(guò)設(shè)置layer的屬性 , 最簡(jiǎn)單的一種,但是很影響性能,一般在正常的開(kāi)發(fā)中使用很少
swift代碼如下:
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 150, height: 150))
imageView.image = UIImage(named: "meinv")
imageView.center = view.center
imageView.layer.cornerRadius = imageView.bounds.size.width / 2
imageView.layer.masksToBounds = true
view.addSubview(imageView)
OC代碼如下:
UIImageView * imageview = [[UIImageView alloc] initWithFrame:CGRectMake(00, 00, 150, 150)];
imageview.image = [UIImage imageNamed:@"meinv"];
imageview.layer.cornerRadius = imageview.bounds.size.width / 2;
imageview.layer.masksToBounds = YES;
imageview.center = self.view.center;
[self.view addSubview:imageview];
第二種方法:使用CAShapeLayer和UIBezierPath設(shè)置圓角
swift代碼如下:
let imageview = UIImageView(frame: CGRect(x: 0, y: 0, width: 150, height: 150))
imageview.center = view.center
imageview.image = UIImage(named: "meinv")
//畫(huà)一個(gè)部分圓角的矩形 rect: 需要畫(huà)的矩形的Frame corners: 哪些部位需要畫(huà)成圓角 cornerRadii: 圓角的Size
let maskPath = UIBezierPath(roundedRect: imageview.bounds, byRoundingCorners: .allCorners, cornerRadii: imageview.bounds.size)
let maskLayer = CAShapeLayer()
maskLayer.frame = imageview.bounds
maskLayer.path = maskPath.cgPath
imageview.layer.mask = maskLayer
view.addSubview(imageview)
OC代碼如下:
UIImageView * imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 150, 150)];
imageview.center = self.view.center;
imageview.image = [UIImage imageNamed:@"meinv"];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageview.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:imageview.bounds.size];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
//設(shè)置大小
maskLayer.frame = imageview.bounds;
//設(shè)置圖形樣子
maskLayer.path = maskPath.CGPath;
imageview.layer.mask = maskLayer;
[self.view addSubview:imageview];
第三種方法:使用貝塞爾曲線(xiàn)UIBezierPath和Core Graphics框架畫(huà)出一個(gè)圓角
swift代碼如下:
let imageview = UIImageView(frame: CGRect(x: 0, y: 0, width: 150, height: 150))
imageview.center = view.center
imageview.image = UIImage(named: "meinv")
// 開(kāi)始對(duì)imageView進(jìn)行畫(huà)圖
UIGraphicsBeginImageContextWithOptions(imageview.bounds.size, false, 0);
// 實(shí)例化一個(gè)圓形的路徑
let path = UIBezierPath(ovalIn: imageview.bounds)
// 進(jìn)行路勁裁切 后續(xù)的繪圖都會(huì)出現(xiàn)在圓形內(nèi) 外部的都被干掉
path.addClip()
imageview.draw(imageview.bounds)
// 取到結(jié)果
imageview.image = UIGraphicsGetImageFromCurrentImageContext()
// 關(guān)閉上下文
UIGraphicsEndImageContext()
view.addSubview(imageview)
OC代碼如下:
UIImageView * imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 150, 150)];
imageview.center = self.view.center;
imageview.image = [UIImage imageNamed:@"meinv"];
//開(kāi)始對(duì)imageView進(jìn)行畫(huà)圖
UIGraphicsBeginImageContextWithOptions(imageview.bounds.size, NO, 0);
//使用貝塞爾曲線(xiàn)畫(huà)出一個(gè)圓形圖
[[UIBezierPath bezierPathWithOvalInRect:imageview.bounds] addClip];
[imageview drawRect:imageview.bounds];
imageview.image = UIGraphicsGetImageFromCurrentImageContext();
//結(jié)束畫(huà)圖
UIGraphicsEndImageContext();
[self.view addSubview:imageview];
以上便是iOS中的三種方式對(duì)圖片進(jìn)行切圓角的方法,如果還有其他的方法,歡迎補(bǔ)充。那么這三張切圓角的方法性能如何呢,我們可以用最簡(jiǎn)單的Xcode模擬器DeBug的進(jìn)行檢測(cè)

最主要的是用Color Blended Layers 和 Color Misaligned Images進(jìn)行檢測(cè),這里copy下這些Color的作用
Color Blended Layers 大概的意思就是圖層顏色混合。
大概意思說(shuō)就是綠色最好,紅色不好。。
Color Misaligned Images 黃色的圖層由于圖層顯示的是被縮放后的圖片。還有些系統(tǒng)Navigation Bar和Tool Bar的背景圖片使用的是拉伸(Streched)圖片,也會(huì)被表示為黃色,這是屬于正常情況,通常無(wú)需修改。這種問(wèn)題一般對(duì)性能影響不大,而是可能會(huì)在邊緣處虛化。
我們看下三種切圓角的方法的Blended和Misaligned
方法一:


很顯然,用cornerRadius去切割圖像,無(wú)論Blended還是Misaligned都是極差的,大大的增加了內(nèi)存的消耗
方法二


使用CAShapeLayer和UIBezierPath設(shè)置圓角的話(huà)知識(shí)有圖片拉伸的問(wèn)題,算起來(lái)是比較好的一種方法
方法三


方法三圖片倒是不拉伸了,但是Blended又有問(wèn)題了
那么有沒(méi)有一種方法是Blended和Misaligned都沒(méi)問(wèn)題的呢?
只要把方法三的一些參數(shù)改變一下,就可以實(shí)現(xiàn)了,在方法三中,有個(gè)方法 UIGraphicsBeginImageContextWithOptions(imageview.bounds.size, false, 0); 其中第二個(gè)參數(shù) true表示透明 false表示不透明,只要把參數(shù)改成true,那么無(wú)論是Blended還是Misaligned都可以實(shí)現(xiàn)我們所預(yù)期的樣子,但是這樣切圓角的時(shí)候圖片四周有黑黑的一層,這是我們介意使用 背景填充 setFill這個(gè)方法根據(jù)圖片背景色進(jìn)行填充,這樣就大功告成了。
UIColor.white.setFill()
UIRectFill(imageview.bounds)
最后的樣子:

