直接设置,离屏渲染
_tagImage.layer.cornerRadius = 2;
_tagImage.layer.masksToBounds = YES;
//imageView.layer.shouldRasterize = YES; //光栅化
使用贝塞尔曲线UIBezierPath
UIImage *image = [UIImage imageNamed:@"tag"];
UIGraphicsBeginImageContextWithOptions(_tagImage.bounds.size, NO, [UIScreen mainScreen].scale);
[[UIBezierPath bezierPathWithRoundedRect:_tagImage.bounds cornerRadius:2] addClip];
[image drawInRect:_tagImage.bounds];
_tagImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
使用CAShapeLayer和UIBezierPath
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_tagImage.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(2, 2)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
maskLayer.frame = _tagImage.bounds;
maskLayer.path = maskPath.CGPath;
_tagImage.layer.mask = maskLayer;