(一)
UIImage * icon = self.groupIconView.image;//[UIImage imageNamed:@""];
//以1.png的图大小为底图
//CGImageRef imgRef = icon.CGImage;
// CGFloat w = CGImageGetWidth(imgRef);
//CGFloat h = CGImageGetHeight(imgRef);
UIImage *img1 = [UIImage imageNamed:@"bg_"];
// CGImageRef imgRef1 = img1.CGImage;
//CGFloat w1 = CGImageGetWidth(imgRef1);
//CGFloat h1 = CGImageGetHeight(imgRef1);
//以1.png的图大小为画布创建上下文
UIGraphicsBeginImageContext(CGSizeMake(100, 100));
[img1 drawInRect:CGRectMake(0, 0, 100, 100)];//先把1.png 画到上下文中
[icon drawInRect:CGRectMake(4, 4, 92, 92)];//再把小图放在上下文中
UIImage *resultImg = UIGraphicsGetImageFromCurrentImageContext();//从当前上下文中获得最终图片
UIGraphicsEndImageContext();//关闭上下文
// CGImageRelease(imgRef);
// CGImageRelease(imgRef1);
icon = resultImg;
应用场景 二维码中间的图标加边框 等等:
// 生成带图片的二维码
[UIImage qrImageWithString:content size:myWidth * qrScale iconImage:icon scale: margin/(myWidth * qrScale) completion:^(UIImage *image) {
weakSelf.qrImageView.image = image;
}
#pragma mark - 带图片二维码(图片指定比例、方形)
+ (void)qrImageWithString:(NSString *)string size:(CGFloat)size iconImage:(UIImage *)iconImage scale:(CGFloat)scale completion:(void (^)(UIImage * image))completion {
// 传入 CenterImgType_Square scale
[UIImage qrImageWithString:string size:size CenterImageType:CenterImgType_CornorRadious iconImage:iconImage scale:scale completion:completion];//CenterImgType_Square 方形 CenterImgType_CornorRadious 切圆角
}
(二)
如果内置小图片要求为圆角,则需要绘图
//绘图圆角
CGRect rect = (CGRect){0.f, 0.f, icon.size};
UIGraphicsBeginImageContextWithOptions(icon.size, NO, UIScreen.mainScreen.scale);
CGContextAddPath(UIGraphicsGetCurrentContext(),
[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:8].CGPath);
CGContextClip(UIGraphicsGetCurrentContext());
[icon drawInRect:rect];
UIImage *output = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
关于绘制这块,系统已经提供了几个内置的方法:
+ (instancetype)bezierPathWithRect:(CGRect)rect;
+ (instancetype)bezierPathWithOvalInRect:(CGRect)rect;
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius; // rounds all corners with the same horizontal and vertical radius
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii;
+ (instancetype)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;
+ (instancetype)bezierPathWithCGPath:(CGPathRef)CGPath;
今天就简单的介绍一下这几个方法
1:画矩形
- (instancetype)bezierPathWithRect:(CGRect)rect;
系统方法,画矩形
rect: 矩形的Frame
- (void)drawRect:(CGRect)rect
{
// 设置线的填充色
[[UIColor redColor] setStroke];
// 新建一个bezier对象,此对象用于绘制矩形,需要传入绘制的矩形的Frame
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRect:CGRectMake(10, 10, 280, 280)];
// 设置线宽度
bezierPath.lineWidth = 10;
// 设置线两头样式
bezierPath.lineCapStyle = kCGLineCapRound;
// 开始绘制
[bezierPath stroke];
}

2:画矩形,圆角矩形
- (instancetype)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius; // rounds all corners with the same horizontal and vertical radius
系统方法,绘制一个圆角的矩形
rect: 矩形的Frame
cornerRadius: 圆角的半径
- (void)drawRect:(CGRect)rect
{
// 设置线的填充色
[[UIColor redColor] setStroke];
// 新建一个bezier对象,此对象用于绘制一个圆角矩形
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, 10, 280, 280)
cornerRadius:30];
// 设置线宽度
bezierPath.lineWidth = 10;
// 开始绘制
[bezierPath stroke];
}

3:画矩形,部分圆角的矩形
- (instancetype)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii;
画一个部分圆角的矩形
rect: 需要画的矩形的Frame
corners: 哪些部位需要画成圆角
cornerRadii: 圆角的Size
- (void)drawRect:(CGRect)rect
{
// 设置线的填充色
[[UIColor redColor] setStroke];
// 新建一个bezier对象,此对象用于绘制一个部分圆角的矩形,左上、右下
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, 10, 280, 280)
byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight
cornerRadii:CGSizeMake(10, 10)];
// 设置线宽度
bezierPath.lineWidth = 10;
// 开始绘制
[bezierPath stroke];
}

4:画圆,内切圆
- (instancetype)bezierPathWithOvalInRect:(CGRect)rect;
画圆,这个方法绘制的是一个矩形的内切圆
rect: 矩形的Frame
- (void)drawRect:(CGRect)rect
{
// 设置线的填充色
[[UIColor redColor] setStroke];
// 新建一个bezier对象,此对象用于绘制内切圆,需要传入绘制内切圆的矩形的Frame
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 10, 280, 280)];
// 设置线宽度
bezierPath.lineWidth = 10;
// 设置线两头样式
bezierPath.lineCapStyle = kCGLineCapRound;
// 开始绘制
[bezierPath stroke];
}

5:画圆弧
- (instancetype)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;
center: 圆心坐标
radius: 圆的半径
startAngle: 绘制起始点角度
endAngle: 绘制终点角度
clockwise: 是否顺时针
- (void)drawRect:(CGRect)rect
{
// 设置线的填充色
[[UIColor redColor] setStroke];
// 新建一个bezier对象,此对象用于绘制一个圆弧
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150)
radius:110
startAngle:0
endAngle:M_PI_2
clockwise:NO];
// 设置线宽度
bezierPath.lineWidth = 10;
// 开始绘制
[bezierPath stroke];
}
