切圓角一直是個(gè)老生長談問題,為什么呢?就是因?yàn)樾蕟栴}選擇一個(gè)高效的渲染方式是關(guān)鍵。
*注:現(xiàn)在我們不用太擔(dān)心這個(gè)問題了從9.0以后所有的切圓角的方案都不會(huì)產(chǎn)生離屏渲染。
下面我們說說切圓角都有哪些方式:
-
設(shè)置視圖的layer屬性:
//設(shè)置視圖layer層的兩個(gè)屬性
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 30, 100, 100)];
imageView.image = [UIImage imageNamed:@"head.png"];
//設(shè)置圓角半徑
imageView.layer.cornerRadius = 15.0f;
//將多余的部分切掉
imageView.layer.masksToBounds = YES;
[self.view addSubview:imageView];
效果圖:

connerRadius.jpg
優(yōu)點(diǎn):方便快捷、代碼簡單。
缺點(diǎn):8.0會(huì)有離屏渲染效率低下,但是9.0做了優(yōu)化以后均是主屏渲染。
-
UIImage切圓角
- (void)imageView:(UIImageView *)imageView cornerRadius:(CGFloat)radius
{
//開始對(duì)imageView進(jìn)行畫圖
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, [UIScreen mainScreen].scale);
//使用貝塞爾曲線畫出一個(gè)圓形圖
[[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:radius] addClip];
[imageView drawRect: imageView.bounds];
imageView.image = UIGraphicsGetImageFromCurrentImageContext();
//結(jié)束畫圖
UIGraphicsEndImageContext();
}
效果圖:

roundedRect.jpg
優(yōu)點(diǎn):不會(huì)產(chǎn)生離屏渲染
系統(tǒng):8.0以后均不會(huì)產(chǎn)生離屏渲染。
-
通過疊加圖層遮擋方式
此方法就是在要添加圓角view上疊加一個(gè)中間鏤空透明的視圖,只對(duì)圓角部分進(jìn)行遮擋,不過同時(shí)也需要遮擋顏色和view背景色一致才行。
原圖片:

headImage.png

masklayer.png
self.view.backgroundColor = [UIColor redColor];
UIImageView *headImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"headImage.png"]];
headImageView.center = CGPointMake(100, 400);
headImageView.size = CGSizeMake(60, 60);
[self.view addSubview:headImageView];
UIImage *image = [UIImage imageNamed:@"masklayer.png"];
UIEdgeInsets insets = UIEdgeInsetsMake(image.size.height/2, image.size.width/2, image.size.height/2, image.size.width/2);
image = [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.x = 0;
imageView.y = 0;
imageView.width = headImageView.width;
imageView.height = headImageView.height;
[headImageView addSubview:imageView];
合成后圖片:

complex.jpg
優(yōu)點(diǎn):此方法也是最優(yōu)解,沒有離屏渲染,沒有額外的CPU計(jì)算。
缺點(diǎn):應(yīng)用范圍有限。
-
通過賽貝爾曲線畫出鏤空layer覆蓋到視圖上
self.view.backgroundColor = [UIColor greenColor];
UIImageView *headImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Image_head"]];
headImageView.center = CGPointMake(100, 400);
headImageView.size = CGSizeMake(60, 60);
[self.view addSubview:headImageView];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, headImageView.width, headImageView.height) cornerRadius:0];
[path appendPath:[[UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, headImageView.width, headImageView.height) cornerRadius:20] bezierPathByReversingPath]];
shapeLayer.path = path.CGPath;
shapeLayer.lineWidth = 0;
shapeLayer.strokeColor = [UIColor whiteColor].CGColor;
shapeLayer.fillColor = [UIColor whiteColor].CGColor;
shapeLayer.contentsScale = 20;
shapeLayer.lineCap = kCALineCapRound;
shapeLayer.strokeStart = 0.0f;
shapeLayer.strokeEnd = 0.1f;
[headImageView.layer addSublayer:shapeLayer];
效果圖:

UNADJUSTEDNONRAW_thumb_667.jpg
優(yōu)點(diǎn):效率最高、內(nèi)存消耗最少、不會(huì)產(chǎn)生離屏渲染、相對(duì)圖層疊加減少了一張覆蓋層圖片。