1,获取上下文
UIGraphicsBeginImageContextWithOptions(CGSizeMake(MAINSCREEN_WIDTH, adjustScaleHeight), YES, scale);
2,绘制图片
[image drawInRect:CGRectMake(0, 0, MAINSCREEN_WIDTH, adjustScaleHeight)];
3,绘制文字
xNSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy]; textStyle.lineBreakMode = NSLineBreakByWordWrapping; textStyle.alignment = NSTextAlignmentCenter; CGRect nameRect = CGRectMake((MAINSCREEN_WIDTH - 80)/2, qr_Y + qr_height + 10, 80, 30); [@"邀请码" drawInRect:nameRect withAttributes:@{NSFontAttributeName:[UIFont qg_regularFontOfSize:22.0f],NSForegroundColorAttributeName:UIColor.textColor3333,NSParagraphStyleAttributeName:textStyle}];4,绘制线条
获取上下文CGContextRef ctx = UIGraphicsGetCurrentContext();
绘制白色背景
xxxxxxxxxxCGRect rect = CGRectMake((MAINSCREEN_WIDTH - 128)/2, qr_Y + qr_height + 50, 128, 30);UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:3];CGContextAddPath(ctx, path.CGPath);CGContextSetFillColorWithColor(ctx, [UIColor colorWithWhite:1.0 alpha:0.8].CGColor); CGContextFillPath(ctx);
5,绘制结束
xxxxxxxxxxUIImage *result = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
x
let ctx = UIGraphicsGetCurrentContext()! ctx.setStrokeColor(UIColor.cyan.cgColor) ctx.setLineWidth(10.0) ctx.addArc(center: CGPoint(x: width/2.0, y: height/2.0), radius: 100, startAngle: 0, endAngle: CGFloat( 2*Double.pi), clockwise: true) ///注意这里 ctx.addLine(to: CGPoint(x: width/2 + 150, y: height/2 )) let path = UIBezierPath(roundedRect: CGRect(x: width/2 - 150, y: height/2 - 150, width: 300, height: 300), cornerRadius:10.0) ctx.addPath(path.cgPath) ctx.closePath() ctx.setFillColor(UIColor.cyan.cgColor) ctx.drawPath(using: CGPathDrawingMode.fill)