zoukankan      html  css  js  c++  java
  • 在drawRect:方法中绘制图片,文字以及Core Graphics 框架的了解

     1 -(void)drawRect:(CGRect)rect{
     2     
     3     // 画同心圆,初始化Bezier对象
     4     UIBezierPath *path = [[UIBezierPath  alloc]init];
     5     float maxRadio = 50.0;
     6     float currentRadio;
     7 
     8     // 设置画笔颜色,Bezier类没有设置线条颜色的属性,但是在OtherView中说可以用UIColor类设置颜色(线条和填充)
     9      [[UIColor redColor] setStroke];
    10     
    11     
    12     for  (currentRadio = maxRadio; currentRadio > 0; currentRadio -= 10) {
    13         // 拿起画笔 ,移动画笔到下一个图形的起点 ,不然会直接移动过去,会有两个图形间会有连接线
    14         [path  moveToPoint:CGPointMake(200+currentRadio, 250)]  ;
    15        
    16         //clockwise 顺时针属性 为yes,表示顺时针画(0-1.5M_PI),NO表示画(1.5M_PI - 2M_PI)
    17         [path addArcWithCenter:CGPointMake(200 , 250) radius:currentRadio startAngle:0   endAngle:2*
    18          M_PI clockwise:YES];
    19         
    20         
    21     }
    22     
    23     // 在uiimage 和nsstring 中系统提供了想用的方法,来绘制。
    24    //图片
    25     UIImage *logoIMage = [UIImage imageNamed:@"3.png"];
    26     [logoIMage drawInRect:CGRectMake(183, 233, 34, 34)];
    27     
    28     // 字符串
    29     NSString * str = [NSString stringWithFormat:@"我是画上去的"];
    /*文本属性
    相应的文本属性有很多,可以参看辣条的http://blog.csdn.net/qq_32531823/article/details/54379623
    */
    30 [str drawInRect:CGRectMake(20, 700, 300, 50) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}]; 31 32 33 //线条宽度 34 // path.lineWidth = 5.0; 35 // 线头样式 无,方头,圆头 36 // path.lineCapStyle = 2; 37 38 39 // 开始画 40 [path stroke]; 41 42 }

    以上这些绘制原理都是基于 Core Graphics 框架完成的,所以有一些 关于绘制很高的要求时,可以去查一查 相关API。

     下面是使用 Core Graphics的简单代码实例, 这个框架是一套提供2D绘图功能的C语言API,使用C结构和C函数模拟了一套面向对象的编程机制,并没有OC中的对象和方法。Core Graphics 中的CGContextRef 负责储存绘画状态(画笔颜色线条之类的)和绘制内容所出空间。

    1 为什么它能储存 绘制内容所储存的空间

           很多Core Graphics 框架中的类型 都带有Ref后缀,这种类型是用来模拟面向对象C结构。Core Graphics “对象”(CGContextRef) 与OC的对象都是在堆上分配内存的,所以再起被创建时,都会返回一个指向对象内存地址的指针。(像CGPoint 这种结构简单,没后后缀的, 就是在栈上分配内存的)。即CGContextRef 是一种结构指针(结构名后加一个 *)。比如说 CGColor (不直接使用)结构 有一个表示CGColor * 的类型 CGColorRef (一般用这个)。

    注意: 带有Ref后缀的类型对象可能拥有指向其它Core Graphics “对象”的强引用指针,而作为其他”对象“的拥有者时,ARC无法识别 这类强引用和 “对象”的所有权,所以必须在使用后手动 release掉它。

    规则:使用名称中带有 creat或者copy的函数创建了一个Core Graphics ”对象“,就必须手动release。

     下面代码就很好示例了

    -(void)drawRect:(CGRect)rect{  
    // 开启图形上下文
        CGContextRef currentCotext =  UIGraphicsGetCurrentContext();
        //设置画笔颜色
        CGContextSetRGBStrokeColor(currentCotext, 1, 0, 0, 1);
        //创建一个可变路径
        CGMutablePathRef  cgpath = CGPathCreateMutable();
        //路径开始点
        CGPathMoveToPoint(cgpath, NULL, 200, 100);
        //添加一条线到点200,200
        CGPathAddLineToPoint(cgpath, NULL, 200, 200);
        //添加路径到图形上下文
        CGContextAddPath(currentCotext, cgpath);
        // 画图形上下文的路径
        CGContextStrokePath(currentCotext);
        // release 掉 路径指针
        CGPathRelease(cgpath);
    }

      

  • 相关阅读:
    Coursera机器学习week11 单元测试
    关于 TypeReference 的解释
    getModifiers 方法解释。
    instanceof isInstance isAssignableFrom 比较
    elasticsearch 基础 语法总结
    kibana 启动 关闭 和进程查找
    MD5 SHA1 SHA256 SHA512 SHA1WithRSA 的区别
    spring boot 项目 热启动
    java zip 压缩文件
    Packet for query is too large (1660 > 1024). You can change this value on the server by setting the max_allowed_packet' variable.
  • 原文地址:https://www.cnblogs.com/DafaRan/p/7298839.html
Copyright © 2011-2022 走看看