图片圆角处理

问到tableView有哪些优化的方法,想必很多人会说到,图片尽量不要圆角处理,特别注意的是,不是说这里的图片不能圆角显示,只是说不能使用setCornerRadiusi对imageview的layer进行圆角处理,网上的解释是,通过设置layer的属性,实现圆角,在iOS9以前这种设置可能会触发离屏渲染,而这是比较消耗性能的,会明显感觉到卡顿。
注意:ios9.0之后对UIImageView的圆角设置做了优化,UIImageView这样设置圆角
不会触发离屏渲染,ios9.0之前还是会触发离屏渲染。而UIButton还是都会触发离屏渲染。

下面有三种设置圆角的方法:

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"keai"]];

    imageView.backgroundColor = [UIColor yellowColor];

    imageView.frame = CGRectMake(100, 100, self.view.frame.size.width - 200, self.view.frame.size.width - 200);

    [self.view addSubview:imageView];

    //    [self setGraphicsCutCirculayWithView:imageView roundedCornersSize:100];

    [self setLayerAndBezierPathCutCircularWithView:imageView roundedCornersSize:100];

}

#pragma mark -  通过设置layer 切圆角,是最常用的,也是最耗性能的

- (void)setLayerCutCirculayWithView:(UIView *) view roundedCornersSize:(CGFloat )cornersSize

{

    view.layer.masksToBounds = YES;

    // 设置圆角半径

    view.layer.cornerRadius = cornersSize;

}

 #pragma mark - 通过layer和bezierPath 设置圆角

- (void)setLayerAndBezierPathCutCircularWithView:(UIView *) view roundedCornersSize:(CGFloat )cornersSize

{

    // 创建BezierPath 并设置角 和 半径 这里只设置了 左上 和 右上

    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(cornersSize, cornersSize)];

    CAShapeLayer *layer = [[CAShapeLayer alloc] init];

    layer.frame = view.bounds;

    layer.path = path.CGPath;

    view.layer.mask = layer;

}

 #pragma mark - 通过Graphics 和 BezierPath 设置圆角

- (void)setGraphicsCutCirculayWithView:(UIImageView *) view roundedCornersSize:(CGFloat )cornersSize

{

    UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 1.0);

    [[UIBezierPath bezierPathWithRoundedRect:view.bounds cornerRadius:cornersSize] addClip];

    [view drawRect:view.bounds];

    view.image = UIGraphicsGetImageFromCurrentImageContext();

    // 结束

    UIGraphicsEndImageContext();

}

@end

最好的是通过Graphics 和 BezierPath 设置圆角
原文地址:https://www.cnblogs.com/jingxin1992/p/7099434.html