平常我們使用的圓角都是使用:
_imgIcon=[[UIImageView alloc] initWithFrame:CGRectMake(KScreenWidth-20-10-43, 18, 42, 42)];
_imgIcon.layer.cornerRadius=21.0;
// _imgIcon.image=[UIImage imageNamed:@"默認(rèn)頭像"];
_imgIcon.clipsToBounds=YES;
[btnIcon addSubview:_imgIcon];
過多的使用可能會(huì)造成GPU性能的損耗厌蔽。
于是我繼續(xù)查資料,發(fā)現(xiàn)圖片的圓角可以將圖片進(jìn)行進(jìn)行重繪,得到一張新的圖片。方法如下:borderW這個(gè)主要設(shè)置頭像的邊框?qū)挾?/p>
-(UIImage *)imageWithBorder:(CGFloat)borderW color:(UIColor *)borderColor image:(UIImage *)image{
//0.加載圖片
// UIImage *image=[UIImage imageNamed:@"阿貍頭像"];
//1.確定邊框?qū)挾?// CGFloat borderW=10;
//2.開啟一個(gè)上下文
CGSize size =CGSizeMake(image.size.width + 2*borderW, image.size.height + 2*borderW);
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
//3.繪制大圓蛹锰。顯示出來
UIBezierPath *path=[UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, size.width, size.height)];
[borderColor set];
[path fill];
//4.繪制一個(gè)小圓,顯示出來
UIBezierPath *clipPath=[UIBezierPath bezierPathWithOvalInRect:CGRectMake(borderW, borderW, image.size.width, image.size.height)];
[clipPath addClip];
//5.把圖片繪制到上下文當(dāng)中
[image drawAtPoint:CGPointMake(borderW, borderW)];
//6.從上下文當(dāng)中取出圖片
UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();
//7.關(guān)閉上下文
UIGraphicsEndImageContext();
return newImage;
}
然后調(diào)用的時(shí)候:
self.imageV.image=[UIImage imageWithBorder:10.0 color:[UIColor greenColor] image:[UIImage imageNamed:@"阿貍頭像"]];
WechatIMG1.jpeg