iOS--切圆角

切圆角一直是个老生长谈问题,为什么呢?就是因为效率问题选择一个高效的渲染方式是关键。
*注:现在我们不用太担心这个问题了从9.0以后所有的切圆角的方案都不会产生离屏渲染。

下面我们说说切圆角都有哪些方式:

  • 设置视图的layer属性:
    //设置视图layer层的两个属性
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 30, 100, 100)];
    imageView.image = [UIImage imageNamed:@"head.png"];
    //设置圆角半径
    imageView.layer.cornerRadius = 15.0f;
    //将多余的部分切掉
    imageView.layer.masksToBounds = YES;
    
    [self.view addSubview:imageView];

效果图:


connerRadius.jpg

优点:方便快捷、代码简单。
缺点:8.0会有离屏渲染效率低下,但是9.0做了优化以后均是主屏渲染。

  • UIImage切圆角
- (void)imageView:(UIImageView *)imageView cornerRadius:(CGFloat)radius
{
    //开始对imageView进行画图
    UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, [UIScreen mainScreen].scale);
    //使用贝塞尔曲线画出一个圆形图
    [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:radius] addClip];
    [imageView drawRect: imageView.bounds];
    
    imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    //结束画图
    UIGraphicsEndImageContext();
}

效果图:


roundedRect.jpg

优点:不会产生离屏渲染
系统:8.0以后均不会产生离屏渲染。

  • 通过叠加图层遮挡方式

此方法就是在要添加圆角view上叠加一个中间镂空透明的视图,只对圆角部分进行遮挡,不过同时也需要遮挡颜色和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

优点:此方法也是最优解,没有离屏渲染,没有额外的CPU计算。
缺点:应用范围有限。

  • 通过赛贝尔曲线画出镂空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

优点:效率最高、内存消耗最少、不会产生离屏渲染、相对图层叠加减少了一张覆盖层图片。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 关于imageView以如下这种最“普通”的切圆角的方法,网上盛传,这样会影响性能,认识不够深,不能理解,百度了到...
    一天卡卡阅读 370评论 2 1
  • 使用frame布局,是完全没问题的,如果想用masonry,就需要让约束布局生效,让视图先渲染出来就可以了,一般做...
    nadou23阅读 8,691评论 5 5
  • 今天接着看《人民的名义》,对李达康这个角色感觉挺有意思的。 首先从目前看的剧情分析看,李达康这个角色主要在于对权利...
    丿子木丨阅读 156评论 0 0
  • 今晚听了一个多小时的写作入门课程,老师主要讲的是写文案的思维:竞争思维、产品思维、用户思维、销售思维,以及思维的运...
    月落April阅读 1,618评论 0 1
  • 描述 在 Android 系统中,广播(Broadcast)是在组件之间传播数据的一种机制,这些组件可以位于不同的...
    pkqgo阅读 1,632评论 1 2