Quartz2D使用

 

使用Quartz2D前  先导入QuartzCore.framework

//划线

//1.获取图形上下文
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    //2.绘图(画线)
    //设置起点
    CGContextMoveToPoint(ctx, 20, 20);
    //设置终点
    CGContextAddLineToPoint(ctx, 200, 300);
    //渲染
    CGContextStrokePath(ctx);

常用拼接路径函数

  • 新建一个起点
    void CGContextMoveToPoint(CGContextRef c, CGFloat x, CGFloat y)

  • 添加新的线段到某个点
    void CGContextAddLineToPoint(CGContextRef c, CGFloat x, CGFloat y)

  • 添加一个矩形
    void CGContextAddRect(CGContextRef c, CGRect rect)

  • 添加一个椭圆
    void CGContextAddEllipseInRect(CGContextRef context, CGRect rect)

  • 添加一个圆弧
    void CGContextAddArc(CGContextRef c, CGFloat x, CGFloat y, CGFloat radius, CGFloat startAngle, CGFloat endAngle, int clockwise)

常用绘制路径函数

  • Mode参数决定绘制的模式
    void CGContextDrawPath(CGContextRef c, CGPathDrawingMode mode)

  • 绘制空心路径
    void CGContextStrokePath(CGContextRef c)

  • 绘制实心路径
    void CGContextFillPath(CGContextRef c)

提示:一般以CGContextDraw、CGContextStroke、CGContextFill开头的函数,都是用来绘制路径的

 

图形上下文栈的操作

  • 将当前的上下文copy一份,保存到栈顶(那个栈叫做”图形上下文栈”)
    void CGContextSaveGState(CGContextRef c)

  • 将栈顶的上下文出栈,替换掉当前的上下文
    void CGContextRestoreGState(CGContextRef c)

矩阵操作

利用矩阵操作,能让绘制到上下文中的所有路径一起发生变化

  • 缩放
    void CGContextScaleCTM(CGContextRef c, CGFloat sx, CGFloat sy)

  • 旋转
    void CGContextRotateCTM(CGContextRef c, CGFloat angle)

  • 平移
    void CGContextTranslateCTM(CGContextRef c, CGFloat tx, CGFloat ty)

简书链接:http://www.jianshu.com/p/0e785269dccc

 

  // 1.开启基于位图的图形上下文

    /**

     *  CGSize size:size

     *  BOOL opaque:透明开关,如果图形完全不用透明,设置为YES以优化位图的存储。

     *  CGFloat scale:缩放比例

     */

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(100, 100), NO, 0.0);

    //设置颜色填满

    [color setFill];

    //绘图(实心)

    UIRectFill(CGRectMake(0, 0, 100, 100));

    //获得画的图形保存为图片

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    //关闭上下文

    UIGraphicsEndImageContext();

 

最后附上天气温度折线图:链接: http://pan.baidu.com/s/1pKWJmPl 密码: b28v

posted @ 2015-12-15 21:01  偏执Dog  阅读(139)  评论(0编辑  收藏  举报