天天看点

iOS学习笔记-052.图形上下文图形上下文

  • 图形上下文
    • 一图形上下文基础
    • 二Quartz2D提供了以下几种类型的
    • 三图形上下文栈的操作
    • 四代码

图形上下文

一、图形上下文基础

图形上下文(Graphics Context):是一个CGContextRef类型的数据

图形上下文的作用

保存绘图信息、绘图状态

决定绘制的输出目标(绘制到什么地方去?)

(输出目标可以是PDF文件、Bitmap或者显示器的窗口上)

iOS学习笔记-052.图形上下文图形上下文

相同的一套绘图序列,指定不同的Graphics Context,就可将相同的图像绘制到不同的目标上

二、Quartz2D提供了以下几种类型的

Bitmap Graphics Context

PDF Graphics Context

Window Graphics Context

Layer Graphics Context

Printer Graphics Context
           
iOS学习笔记-052.图形上下文图形上下文

三、图形上下文栈的操作

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

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

四、代码

//
//  WMView.m
//  03_UIView43_图形上下文栈
//
//  Created by 杞文明 on 2016/04/14 07:16:43   星期四
//  Copyright © 2016年 杞文明. All rights reserved.
//

#import "WMView.h"

@implementation WMView


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {

    //1.获取图形上下文栈
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    //保存一份最纯洁的图像上下文-------
    CGContextSaveGState(ctx);

    //2.绘制
    //---------第一条线-------
    CGContextMoveToPoint(ctx, , );
    CGContextAddLineToPoint(ctx, , );
    CGContextSetLineWidth(ctx, );
    [[UIColor redColor]set];
    //3.渲染
    CGContextStrokePath(ctx);

    //---------第二条线-------
    // 还原开始保存的那份最纯洁的图形上下文
    CGContextRestoreGState(ctx);
    CGContextMoveToPoint(ctx, , );
    CGContextAddLineToPoint(ctx, , );
    CGContextStrokePath(ctx);
}

@end
           

五、图示

iOS学习笔记-052.图形上下文图形上下文