第一种方法 直接设置layer的属性
这种方法比较简单,代码量较少,但是操作layer肯定会影响性能,会造成离屏渲染。
#pragma mark - 通过设置layer 切圆角
- (void)setLayerCutCirculayWithView:(UIView *) view
{
view.layer.masksToBounds = YES;
// 设置圆角半径
view.layer.cornerRadius = 5.0f;
}
第二种方法 使用CAShapeLayer和UIBezierPath来设置圆角
这种方法的优点:可以操作任何一个角(左上,右上,左下,右下),并且消耗内存较小,渲染较快。
缺点:操作了layer,对性能有影响,有离屏渲染。
- (void)setLayerAndBezierPathCutCircularWithView:(UIView *) view
{
// 创建BezierPath 并设置角 和 半径 这里只设置了 左上 和 右上
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)];
CAShapeLayer *layer = [[CAShapeLayer alloc] init];
layer.frame = view.bounds;
layer.path = path.CGPath;
view.layer.mask = layer;
}
第三种方法 使用Core Graphics框架 和 UIBezierPath 画出一个圆角
优点:没有操作layer 性能较高。
- (void)setGraphicsCutCirculayWithView:(UIImageView *) view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 1.0);
[[UIBezierPath bezierPathWithRoundedRect:view.bounds cornerRadius:5] addClip];
[view drawRect:view.bounds];
view.image = UIGraphicsGetImageFromCurrentImageContext();
// 结束
UIGraphicsEndImageContext();
}