Day-06

作者: 清杨程 | 来源:发表于2019-06-04 16:17 被阅读0次

第一节 画直线

-知识点
  //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();

相关文章

  • Day-06

    第一节 画直线 第二节 画曲线 第三节 画矩形线 第四节 画圆 第五节 画椭圆 第六节 画弧形、扇形 ...

  • day-06 作业

    1.已知一个列表,求列表中心元素。 2已知一个列表,求所有元素和 3.已知一个列表,输出所有奇数下标元素 4.已知...

  • day-06 作业

    1.已知一个列表,求列表中心元素。 2.已知一个列表,求所有元素和。 3.已知一个列表,输出所有奇数下标元素。 4...

  • Day-06 列表

    1. 什么是列表(list) 列表是python提供的容器型数据类型。可变并且有序的可变 - 列表中的每个元素的值...

  • Day-06 练习

    1.已知一个列表,求列表中心元素。 2.已知一个列表,求所有元素和。 3.已知一个列表,输出所有奇数下标元素。 4...

  • 日更38/100:善待“调节回路”,方能与狼共舞

    善待“调节回路”,方能与狼共舞 ——精听《刘润·商业洞察力30讲》&精读《系统之美》DAY-06 《刘润·商业洞察...

  • day-06 list总结

    1.什么是列表(list):列表是python提供的容器形数据类型。:可变的 --- 列表中的每个元素的值可变,列...

  • 2019-01-15 无题

    Day-06 明天就是复盘日了,我时刻提醒着我自己我是技能官,我应该做到把自己的东西分享出来,思维导图我做到了。 ...

  • [Python] (Day-06) - 字符串

    字符串是 Python 中最常用的数据类型。 我们可以使用引号''或""来创建字符串。 访问字符串中的值 格式:字...

  • Java学习day-06:嵌套循环

    一、嵌套循环专题 1.什么是嵌套循环? 在一个循环语句内部再嵌套一个或多个循环,称为嵌套循环。while、do...

网友评论

      本文标题:Day-06

      本文链接:https://www.haomeiwen.com/subject/mmcrxctx.html