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

图形上下文

一、图形上下文基础

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

图形上下文的作用

保存绘图信息、绘图状态
决定绘制的输出目标(绘制到什么地方去?)
(输出目标可以是PDF文件、Bitmap或者显示器的窗口上)
这里写图片描述

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

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

Bitmap Graphics Context

PDF Graphics Context

Window Graphics Context

Layer Graphics Context

Printer Graphics Context

这里写图片描述

三、图形上下文栈的操作

将当前的上下文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, 20, 20);
    CGContextAddLineToPoint(ctx, 400, 700);
    CGContextSetLineWidth(ctx, 10);
    [[UIColor redColor]set];
    //3.渲染
    CGContextStrokePath(ctx);

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

@end

五、图示
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值