iOS 实现图片圆角的几种方式

第一种方法:通过设置layer的属性

最简单的一种,但是很影响性能,一般在正常的开发中使用很少.

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
   //只需要设置layer层的两个属性
   //设置圆角
    imageView.layer.cornerRadius = imageView.frame.size.width / 2;
   //将多余的部分切掉
    imageView.layer.masksToBounds = YES;
    [self.view addSubview:imageView];

第二种方法:使用Core Graphics框架画出一个圆角

@interface UIImage(YYPClip)

- (UIImage *)drawCircleImage;

@end

@implementation UIImage(YYPClip)

- (UIImage *)drawCircleImage {
    CGFloat side = MIN(self.size.width, self.size.height);
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(side, side), false, [UIScreen mainScreen].scale);
    CGContextAddPath(UIGraphicsGetCurrentContext(),
                    [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, side, side)].CGPath);
    CGContextClip(UIGraphicsGetCurrentContext());
    CGFloat X = -(self.size.width - side) / 2.f;
    CGFloat Y = -(self.size.height - side) / 2.f;
    [self drawInRect:CGRectMake(X, Y, self.size.width, self.size.height)];
    CGContextDrawPath(UIGraphicsGetCurrentContext(), kCGPathFillStroke);
    UIImage *output = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return output;
}

@end

//在需要圆角时调用如下
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    UIImage *img = [[UIImage imageNamed:@"order"] drawCircleImage];
    dispatch_async(dispatch_get_main_queue(), ^{
        view.image = img;
    });
});

第三种方法:使用CAShapeLayer和UIBezierPath设置圆角

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    imageView.image = [UIImage imageNamed:@"1"];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:imageView.bounds.size];

    CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
    //设置大小
    maskLayer.frame = imageView.bounds;
    //设置图形样子
    maskLayer.path = maskPath.CGPath;
    imageView.layer.mask = maskLayer;
    [self.view addSubview:imageView];

以上三种方法来自简书链接 http://www.jianshu.com/p/e97348f42276
这个链接的文章里说第三种方法最好 对内存消耗最少,可是在另一篇比较权威的文章http://www.jianshu.com/p/57e2ec17585b 《iOS-离屏渲染详解》里说第三种方法会使用到mask属性,会离屏渲染,不仅这样,还曾加了一个 CAShapLayer对象.着实不可以取。并指出第二种方法比较可取。另外还提出了第四种方法。
第四种方法:使用带圆形的透明图片.(需要UI做一个切图)

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    imageView.image = [UIImage imageNamed:@"美国1.jpeg"];
    UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    imageView1.image = [UIImage imageNamed:@"圆形白边中空图"];
    [self.view addSubview:imageView];
    [self.view addSubview:imageView1];

最好最好的方法应该是第四种了,虽然比较笨, 但是不会引发离屏渲染,对内存消耗会比较小。

更新:SDWebImage中提供了切圆角图片的方法,在UIImage + Transform类别里

- (nullable UIImage *)sd_roundedCornerImageWithRadius:(CGFloat)cornerRadius
                                              corners:(SDRectCorner)corners
                                          borderWidth:(CGFloat)borderWidth
                                          borderColor:(nullable UIColor *)borderColor;

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

推荐阅读更多精彩内容

  • 一般我们在iOS开发的过程中设置圆角都是如下这样设置的。 所有如果要高性能的设置圆角就需要找另外的方法了。下面是我...
    齐滇大圣阅读 15,063评论 29 98
  • 一般我们在iOS开发的过程中设置圆角都是如下这样设置的。 这样设置会触发离屏渲染,比较消耗性能。比如当一个页面上有...
    zgsddzwj阅读 624评论 0 1
  • 对图片进行圆角处理会相比于直角,它更加柔和优美,是一种很常见的视图效果,在APP中常用于对用户头像的美化,但是设置...
    打瞌睡de小男孩阅读 6,083评论 4 22
  • demo地址 一:相对简便的圆角图片的实现方式 由于这样的处理机制是GPU在当前缓冲区以外新开辟一个渲染缓冲区进行...
    雷鸣1010阅读 2,886评论 0 7
  • 前段时间我办了一张健身卡,从农村搬到城市,这是我第一个急切要做的事情,健身,在农村里,基础设施太差,最多只能去乡间...
    简小商阅读 359评论 0 2