Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
oc 高效绘制圆角
- (UIImage*)lj_imageAddCornerWithRadius:(CGFloat)radius byRoundingCorners:(UIRectCorner)corners borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor imageSize:(CGSize)size {
// 1. 创建一个画布
CGRect rect = CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
// 2. 获取图形上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 3. 计算 path 圆角
UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)];
CGContextAddPath(ctx,path.CGPath);
CGContextClip(ctx);
// 绘制出图片
[self drawInRect:rect];
// 4. 添加 border
if (borderWidth > 0 && borderColor) {
CGContextSaveGState(ctx);
[borderColor setStroke];
path.lineWidth = borderWidth;
[path stroke];
CGContextRestoreGState(ctx);
}
// 获取图片
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment