iOS使用UIBezierPath画圆角

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

这种方法简单,但是很影响性能,特别是在UIcollectionView中展示大量圆角图片,一般在正常的开发中使用很少

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

 第二方法:使用CAShapeLayer和UIBezierPath设置圆角,种最好,对内存的消耗最少,而且渲染快速

    UIImageView *view = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    view.backgroundColor = [UIColor redColor];
    [self.view addSubview:view];
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectEdgeLeft | 
UIRectCornerBottomLeft cornerRadii:CGSizeMake(10, 10)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init]; maskLayer.frame = view.bounds; maskLayer.path = path.CGPath; view.layer.mask = maskLayer;

 UIRectEdgeLeft 可以根据需求设置需要设置圆角的位置。

 

 

posted @ 2016-12-27 15:15  Crazy_ZY  阅读(5698)  评论(0编辑  收藏  举报