iOS高效简易添加圆角

4,773 阅读2分钟

  iOS的圆角是一个永恒且艰巨的问题,苹果也在iOS 9中优化了cornerRadius属性,将帧率提高了不少。但是,圆角在iOS程序中使用频率之高,要求我们必须找到更好的方式去解决这个问题。我看了许多高效添加圆角的博客,总结了处理圆角的方法。

如何设置圆角

  iOS中圆角的添加莫非以下三种模式:

  1. 设置视图的layer.cornerRadius属性
imageView.layer.cornerRadius = imageView.frame.size.width / 2;
imageView.layer.masksToBounds = YES;
  1. ✘✘✘ 使用CAShapeLayer和UIBezierPath设置圆角
UIBezierPath *maskBezierPath = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:imageView.bounds.size];
CAShapeLayer *maskShapeLayer = [[CAShapeLayer alloc]init];
maskShapeLayer.frame = imageView.bounds;
maskShapeLayer.path = maskBezierPath.CGPath;
imageView.layer.mask = maskShapeLayer;
  1. 使用UIBezierPath和CoreGraphics框架画出一个圆角
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, [UIScreen mainScreen].scale);
[[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.frame.size.width] addClip];
[imageView drawRect:imageView.bounds];
imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

性能的影响

  1. 设置视图的layer.cornerRadius属性。   第一种方式适用于页面中圆角内容较少的情况,例如个人中心页面,只有头像用到了圆角,那么大费周折的去裁剪、绘制一个圆角远不如两个代码设置一下cornerRedius属性来的快,并且对性能的损耗几乎可以忽略。

  2. ✘✘✘ 使用CAShapeLayer和UIBezierPath设置圆角。   这种方式同样是修改了视图的layer层,在视图上方添加一层mask遮罩,使视图看上去为圆角,但是经过各个iOS开发者的实践测试,用这种方式对性能的损耗甚至超过了第一种方式,这种吃力不好讨好的事情,我们拒绝使用它!

  3. 使用UIBezierPath和CoreGraphics框架画出一个圆角。   如果你使用了tableView、collectionView,想给它们的cell里添加圆角,或大量给控件们添加了圆角,那再去使用第1种方式何止cornerRadius属性就不太合适了,过量消耗的内存会让你机器的帧数从50、60降低到10、20,这是一件非常影响用户体验的事情,这时这种方法就非常靠谱了。   使用UIBezierPath和CoreGraphics的方式既不会操作layer层,也能够高效的添加圆角。接下来就为UIImageView写一个分类,方便在项目里使用就好。

使用第三种方法使用分类添加圆角的代码

  写一个UIImageView的分类,使用下面代码添加,确保添加圆角时,imageView.image不为nil即可(因为方法的本质是裁剪UIImage)。

[imageView quickSetCornerRadius:50];

UIImageView+cornerRadius.h

#import <UIKit/UIKit.h>

@interface UIImageView (cornerRadius)

- (void)quickSetCornerRadius:(CGFloat)cornerRadius;

@end

@interface UIImage (cornerRadius)

- (UIImage *)imageAddCornerWithRadius:(CGFloat)radius andSize:(CGSize)size;

@end

UIImageView+cornerRadius.m

#import "UIImageView+cornerRadius.h"

@implementation UIImageView (cornerRadius)

- (void)quickSetCornerRadius:(CGFloat)cornerRadius
{
   self.image = [self.image imageAddCornerWithRadius:cornerRadius andSize:self.bounds.size];
}

@end

@implementation UIImage (cornerRadius)

- (UIImage *)imageAddCornerWithRadius:(CGFloat)radius andSize:(CGSize)size
{
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    
    UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(radius, radius)];
    CGContextAddPath(contextRef,path.CGPath);
    CGContextClip(contextRef);
    [self drawInRect:rect];
    CGContextDrawPath(contextRef, kCGPathFillStroke);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

@end