美文网首页
Quartz2D--基本画图(线、图形、图片、文字)

Quartz2D--基本画图(线、图形、图片、文字)

作者: i诺离 | 来源:发表于2017-11-17 09:59 被阅读15次
  • 画直线
- (void)drawLine{
    NSLog(@"%@",NSStringFromCGRect(self.bounds));
    
    //1.取得一个跟View相关联的上下文.
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //2.描述路径
    UIBezierPath *path = [UIBezierPath bezierPath];
    //2.1.设置起点
    [path moveToPoint:CGPointMake(10, 100)];
    //2.1.添加一根线到某个点
    [path addLineToPoint:CGPointMake(200, 20)];
    
    //一个路径上面可以画多条线
    //    [path moveToPoint:CGPointMake(10, 150)];
    //    [path addLineToPoint:CGPointMake(200, 100)];
    
    //把上一条线的终点当作是下一条线的起点.
    [path addLineToPoint:CGPointMake(150, 200)];
    
    
    //设置上下文的状态
    //设置线的宽度
    CGContextSetLineWidth(ctx, 10);
    //设置线的连接样式
    CGContextSetLineJoin(ctx, kCGLineJoinBevel);
    //设置顶角的样式
    CGContextSetLineCap(ctx, kCGLineCapRound);
    //设置线的颜色
    [[UIColor greenColor] setStroke];
    
    
    //3.把路径添加到上下文
    CGContextAddPath(ctx, path.CGPath);
    //4.把上下文的内容显示View fill stroke
    CGContextStrokePath(ctx);
}
  • 添加曲线
- (void)drawQuadCurve{

    //1.获取跟View相关联的上下文
    CGContextRef ctx =  UIGraphicsGetCurrentContext();
    //2.描述路径
    UIBezierPath *path = [UIBezierPath bezierPath];
    //2.1设置起点
    [path moveToPoint:CGPointMake(20, 200)];
    //2.2添加一条曲线到某个点.
    [path addQuadCurveToPoint:CGPointMake(200, 200) controlPoint:CGPointMake(100, 10)];
    
    CGContextSetLineWidth(ctx, 10);
    
    //3.把路径添加到上下文
    CGContextAddPath(ctx, path.CGPath);
    //4.把上下文的内容显示出来.
    CGContextStrokePath(ctx);
}
  • 画矩形
- (void)drawrect{

    //画矩形.
    //1.获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //2.描述路径
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(50, 50, 100, 100)];
    //3.把路径添加到上下文
    CGContextAddPath(ctx, path.CGPath);
    
    [[UIColor yellowColor] set];
    
    //4.把上下文的内容显示
    CGContextFillPath(ctx);
}
//cornerRadius:圆角半径--> 可以画圆
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(50, 50, 100, 100) cornerRadius:20];
[path stroke];
  • 画椭圆
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 100, 100)];
[[UIColor blueColor] set];
//[path fill];
   [path stroke];
  • 画圆的弧线->扇形->圆
//ArcCenter:圆心
//radius:圆的半径
//startAngle:开始角度//起始点圆的最右侧.
//endAngle:截至角度
//clockwise:顺时针还是逆时针 YES:顺时针 NO:逆时针
    
CGPoint center = CGPointMake(rect.size.width * 0.5, rect.size.height * 0.5);
CGFloat radius = rect.size.width * 0.5 - 10;
CGFloat startA = 0;
CGFloat endA = -M_PI_2;
    
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:NO];
    
[path addLineToPoint:center];
    
[[UIColor blueColor] set];
//封闭路径
//[path closePath];

//fill,会自动把路径给关闭
[path fill];
  • 将图片直接画在view上
- (void)drawRect:(CGRect)rect {    
    //1.加载图片
    UIImage *image = [UIImage imageNamed:@"001"];
    
    //绘制出来的图片,是保持原来图片
//        [image drawAtPoint:CGPointZero];
    //把图片填充到这个rect当中.
//        [image drawInRect:rect];
    //添加裁剪区域 .把超区裁剪区域以外都裁剪掉
//    UIRectClip(CGRectMake(0, 0, 50, 50));
//    [image drawAsPatternInRect:self.bounds];
    
    [[UIColor blueColor] set];
    UIRectFill(CGRectMake(10, 10, 100, 100));
}
  • 画文字
- (void)drawText{
    NSString *str = @"小码哥小码哥小码哥小码哥";
    
    //AtPoint:文字所画的位置
    //withAttributes:描述文字的属性.
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    //设置文字大小
    dict[NSFontAttributeName] = [UIFont systemFontOfSize:50];
    
    //设置文字颜色
    dict[NSForegroundColorAttributeName] = [UIColor greenColor];
    //设置描边宽度
    dict[NSStrokeWidthAttributeName] = @2;
    //设置描边颜色
    dict[NSStrokeColorAttributeName] = [UIColor blueColor];

    
    //设置阴影
    NSShadow *shadow = [[NSShadow alloc] init];
    //设置阴影的便宜量
    shadow.shadowOffset = CGSizeMake(10, 10);
    //设置阴影颜色
    shadow.shadowColor = [UIColor greenColor];
    //设置阴影模糊程序
    shadow.shadowBlurRadius = 1;
    dict[NSShadowAttributeName] = shadow;
    //不会自动换行
    [str drawAtPoint:CGPointZero withAttributes:dict];
    //会自动换行.
    [str drawInRect:self.bounds withAttributes:dict];
}

  • 图文上下文栈
- (void)drawRect:(CGRect)rect {

    //1.获取跟View相关联的上下文
   CGContextRef ctx = UIGraphicsGetCurrentContext();
    //2.描述路径
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    //添加横线
    [path moveToPoint:CGPointMake(10, 150)];
    [path addLineToPoint:CGPointMake(290, 150)];

    //把当前的状态保存到图片上下文状态栈里.
    CGContextSaveGState(ctx);
  
    [[UIColor redColor] set];
    CGContextSetLineWidth(ctx, 10);
  
    //3.把路径添加到上下文
    CGContextAddPath(ctx, path.CGPath);

    //4.把上下文的内容渲染出来.
    CGContextStrokePath(ctx);

    path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(150, 10)];
    [path addLineToPoint:CGPointMake(150, 290)];

    //恢复上下文状态栈
    CGContextRestoreGState(ctx);
  
    //3.把路径添加到上下文
    CGContextAddPath(ctx, path.CGPath);
    
    //4.把上下文的内容渲染出来.
    CGContextStrokePath(ctx);
}
  • 上下文矩阵操作
- (void)drawRect:(CGRect)rect {
    
    //1.获得跟View相关联的上下文
    CGContextRef ctx =  UIGraphicsGetCurrentContext();
    //2.描述路径
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-100, -50, 200, 100)];
    [[UIColor redColor] set];
    
    //上下文的矩阵操作,必须得要在添加路径之前做操作.
    //平移
    CGContextTranslateCTM(ctx, 100, 50);
    
    //缩放 当为负数为朝向改方向反转,Quartz坐标系和UIView坐标系不一样,对从pdf获取的图片需要进行处理
    CGContextScaleCTM(ctx, 0.5, 0.5);
    
    //旋转
    CGContextRotateCTM(ctx, M_PI_4);
    
    //3.把路径 添加到当前上下文
    CGContextAddPath(ctx, path.CGPath);

    //4.把上下文的内容渲染出来.
    CGContextFillPath(ctx); 
}

相关文章

  • Quartz2D--基本画图(线、图形、图片、文字)

    画直线 添加曲线 画矩形 画椭圆 画圆的弧线->扇形->圆 将图片直接画在view上 画文字 图文上下文栈 上下文...

  • ios绘图基础

    ios常见的图形绘制 画线 画圆、圆弧 画矩形,画椭圆,多边形 画图片 画文字 1:ios绘图基础 几个基本的概念...

  • HTML5-Canvas的基本功能

    利用 Canvas 的 API,展示一些基本图形的绘制及操作方法,包括画线、画图、文字操作及图片操作等。(内含代码...

  • CGContextRef类画图形、文字、图片

    - (void)drawRect:(CGRect)rect { // Drawing code //获取上下文(画...

  • day10管理系统

    文件基本操作 显示文字 显示图片 显示图形 import pygame if name == 'main':pyg...

  • pygame基本操作

    1、pygame基本操作 2、显示图片 3、形变 4、显示文字 5、显示图形 6、事件

  • 计算机视觉 OpenCV (3)

    今天我们主要介绍如何使用 OpenCV 来在图片上绘制几何图形、线、矩形、圆以及添加文字。 在图片上绘制直线 li...

  • React 绘制图形

    最近项目中有一个需求, 在提供的图片上画出图形区域,并且图形可以编辑改变。 问题点:1、图形为多边形,画图需要ca...

  • canvas画图(文字换行,图片)

    图片 调用 文字(可换行) 调用 时间 layer数据

  • 2019-01-16正则和pygame

    一复习昨天的内容 二, 贪婪 三, re模块 四,pygame 游戏的基本框架 五, 显示图片 六, 显示文字 七图形

网友评论

      本文标题:Quartz2D--基本画图(线、图形、图片、文字)

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