Skip to content

Instantly share code, notes, and snippets.

@manajay
Created November 10, 2018 09:00
Show Gist options
  • Save manajay/4ec9de0d95b1925950d621806bcfd86f to your computer and use it in GitHub Desktop.
Save manajay/4ec9de0d95b1925950d621806bcfd86f to your computer and use it in GitHub Desktop.
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