第一节 画直线
-知识点
//1.获取一个跟view相关联的上下文
CGContextRef ctf = UIGraphicsGetCurrentContext();
//2.描述路径
UIBezierPath *bizePath = [UIBezierPath bezierPath];
//2.1设置起点
[bizePath moveToPoint:CGPointMake(10, 200)];
//2.2设置终点
[bizePath addLineToPoint:CGPointMake(200, 10)];
//2.3一个路径添加多条线
//2.3.1设置起点
[bizePath moveToPoint:CGPointMake(10, 240)];
//2.3.2设置终点
[bizePath addLineToPoint:CGPointMake(200, 50)];
//2.4把上条线的终点作为起点
[bizePath addLineToPoint:CGPointMake(100, 250)];
//2.5设置状态
//2.5.1设置宽度
CGContextSetLineWidth(ctf, 15);
//2.5.1设置连接方式
CGContextSetLineJoin(ctf, kCGLineJoinMiter);
//2.5.2设置颜色
[[UIColor greenColor] setStroke];
//3.把路径添加到上下文
CGContextAddPath(ctf, bizePath.CGPath);
//4.把上下文的m内容显示View
CGContextStrokePath(ctf);
-案例<十字架>
CGContextRef ctf = UIGraphicsGetCurrentContext();
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(10, 150)];
[path addLineToPoint:CGPointMake(290, 150)];
//把当前的状态保存到图片上下文状态栈
CGContextSaveGState(ctf);
[[UIColor redColor] set];
CGContextSetLineWidth(ctf, 10);
CGContextAddPath(ctf, path.CGPath);
CGContextStrokePath(ctf);
path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(150, 10)];
[path addLineToPoint:CGPointMake(150, 290)];
[[UIColor blueColor] set];
CGContextSetLineWidth(ctf, 5);
CGContextAddPath(ctf, path.CGPath);
CGContextStrokePath(ctf);
第二节 画曲线
-知识点
CGContextRef ctf = UIGraphicsGetCurrentContext();
UIBezierPath *bizerPath = [UIBezierPath bezierPath];
[bizerPath moveToPoint:CGPointMake(10, 200)];
//添加一条曲线到某个点,参数control:控制点
[bizerPath addQuadCurveToPoint:CGPointMake(200, 200) controlPoint:CGPointMake(100, 10)];
[[UIColor yellowColor] setStroke];
CGContextSetLineWidth(ctf, 5);
CGContextAddPath(ctf, bizerPath.CGPath);
CGContextStrokePath(ctf);
第三节 画矩形线
-知识点
//第一种
CGContextRef ctf = UIGraphicsGetCurrentContext();
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRect:CGRectMake(10, 10, 200, 100)];
CGContextSetLineWidth(ctf, 5);
[[UIColor redColor] set];
CGContextAddPath(ctf, bezierPath.CGPath);
CGContextFillPath(ctf);
//第二种 UIKit封装
//UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, 10, 100, 100) cornerRadius:10];
//[[UIColor redColor] setFill];
//[bezierPath fill];
第四节 画圆
-知识点
//第一种 Radius设置高的一半
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, 10, 100, 100) cornerRadius:50];
[bezierPath stroke];
//第一种 长和高相等
//UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 10,100, 50)];
//[bezierPath stroke];
第五节 画椭圆
-知识点
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 10,100, 100)];
[bezierPath stroke];
第六节 画弧形、扇形
-知识点
//参数ArcCenter:圆心 参数radius:圆的半径 参数startAngle:开始角度(默认水平右侧) 参数endAngle:截止角度 参数clockwise:YES顺时针 NO逆时针
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 100) radius:50 startAngle:0 endAngle:M_PI_2 clockwise:YES];
[bezierPath addLineToPoint:CGPointMake(100, 100)];
[[UIColor blueColor] setStroke];
[bezierPath closePath];
[bezierPath stroke];
第七节 画图
-知识点
UIImage *image = [UIImage imageNamed:@"imageName"];
//绘制图片保持原来图片
//[image drawAtPoint:CGPointZero];
//把整个图片填充Rect当中
//[image drawInRect:rect];
//剪切
//UIRectClip(CGRectMake(0, 0, 60, 60));
//图片进行平铺
[image drawAsPatternInRect:self.bounds ];
//填充
UIRectFill(CGRectMake(0, 0, 100, 100));
第八节 画文字
-知识点
NSMutableDictionary *txtDict = [NSMutableDictionary dictionary];
txtDict[NSFontAttributeName] = [UIFont systemFontOfSize:70];//设置字体大小
txtDict[NSForegroundColorAttributeName] = [UIColor yellowColor];//设置字体颜色
txtDict[NSBackgroundColorAttributeName] = [UIColor greenColor];//设置背景色
txtDict[NSStrokeWidthAttributeName] = @3;//设置描边宽度
NSShadow *shadow = [[NSShadow alloc] init];//设置阴影
shadow.shadowOffset = CGSizeMake(10, 10);//设置阴影偏移量
shadow.shadowBlurRadius = 5;//设置阴影模糊程度
txtDict[NSShadowAttributeName] = shadow;
NSString *txtString = @"小码哥小马哥";
//[txtString drawAtPoint:CGPointZero withAttributes:txtDict];//不能自动换行
[txtString drawInRect:self.bounds withAttributes:txtDict];//自动换行
第九节 绘制平移、缩放、旋转
-知识点
CGContextRef txf = UIGraphicsGetCurrentContext();
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-100, -50, 200, 100)];
[[UIColor redColor] set];
//平移
CGContextTranslateCTM(txf, 100, 50);
//缩放
CGContextScaleCTM(txf, 0.5, 0.5);
//旋转
CGContextRotateCTM(txf, M_PI_4);
CGContextAddPath(txf, path.CGPath);
CGContextFillPath(txf);
第十节 给图片加水印
-知识点
//1、加载图片
UIImage *image = [UIImage imageNamed:@"imagename"];
//2、生成图片
//参数1:size:开启一个多大图片上下文。
//参数2:opaque:不透明
//参数3:scale:缩放度
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0);
//3、把图片给绘制图片上下文 Point:绘制点(0,0)
[image drawAtPoint:CGPointZero];
//4、绘制文字
NSString *str = @"维谷";
NSMutableDictionary *textDict = [NSMutableDictionary dictionary];
textDict[NSFontAttributeName] = [UIFont systemFontOfSize:50];
textDict[NSForegroundColorAttributeName] = [UIColor blueColor];
[str drawAtPoint:CGPointZero withAttributes:textDict];
//5、生成图片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//6、手动关闭上下文
UIGraphicsEndImageContext();
第十二节 圆形图片裁剪
-知识点
//1、加载图片
UIImage *image = [UIImage imageNamed:@"imagename"];
//2、生成一个跟图片相同大小图片上下文
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0);
//3、添加一个圆形裁剪区域
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, image.size.width, image.size.width)];
//4、把路径设置成裁剪区域
[path addClip];
//5、把图片绘制到图片上下文
[image drawAtPoint:CGPointZero];
//6、生成一张图片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//7、关闭图片上下文
UIGraphicsEndImageContext();
网友评论