首页 > 代码库 > UIImage分类,设置边框
UIImage分类,设置边框
#import "UIImage+image.h"
@implementation UIImage (image)
+ (UIImage *)imageWithBorder:(CGFloat)borderW color:(UIColor *)color image:(NSString *)imageName {
// 增加边框 生成边框的宽度 w = image.width + 2*borderW 高度同理
UIImage *image = [UIImage imageNamed:imageName];
// 开启上下文
CGSize size = CGSizeMake(image.size.width + 2 * borderW, image.size.height + 2 * borderW);
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
// 绘制大圆,显示出来
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, size.width, size.height)];
[[UIColor redColor] set];
[path fill];
// 绘小圆
UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(borderW, borderW, image.size.width, image.size.height)];
// 设置为裁剪路径
[clipPath addClip];
// 画图
[image drawAtPoint:CGPointMake(borderW, borderW)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
@implementation UIImage (image)
+ (UIImage *)imageWithBorder:(CGFloat)borderW color:(UIColor *)color image:(NSString *)imageName {
// 增加边框 生成边框的宽度 w = image.width + 2*borderW 高度同理
UIImage *image = [UIImage imageNamed:imageName];
// 开启上下文
CGSize size = CGSizeMake(image.size.width + 2 * borderW, image.size.height + 2 * borderW);
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
// 绘制大圆,显示出来
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, size.width, size.height)];
[[UIColor redColor] set];
[path fill];
// 绘小圆
UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(borderW, borderW, image.size.width, image.size.height)];
// 设置为裁剪路径
[clipPath addClip];
// 画图
[image drawAtPoint:CGPointMake(borderW, borderW)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
UIImage分类,设置边框
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。