美文网首页iOS开发那点儿事
iOS 绘图几种场景下的上下文CGContextRef

iOS 绘图几种场景下的上下文CGContextRef

作者: d4d5907268a9 | 来源:发表于2022-07-05 16:16 被阅读0次
image.png image.png
  1. 上面的图可以看出iOS中绘图框架与其之前的关系;

作为初学者,很容易被UIKit和Core Graphics两个支持绘图的框架迷惑。
有时候需要绘图的时候,别说怎么绘图了,就是连起手都不会,第一行代码都不知道怎么写。下面简单介绍下,有哪些绘图场景,以及如何开始绘图。

UIKit
像UIImage、NSString(绘制文本)、UIBezierPath(绘制形状)、UIColor都知道如何绘制自己。这些类提供了功能有限但使用方便的方法来让我们完成绘图任务。一般情况下,UIKit就是我们所需要的。使用UiKit,你只能在当前上下文中绘图,所以如果你当前处于UIGraphicsBeginImageContextWithOptions函数或drawRect:方法中,你就可以直接使用UIKit提供的方法进行绘图。如果你持有一个context:参数,那么使用UIKit提供的方法之前,必须将该上下文参数转化为当前上下文。幸运的是,调用UIGraphicsPushContext 函数可以方便的将context:参数转化为当前上下文,记住最后别忘了调用UIGraphicsPopContext函数恢复上下文环境。

Core Graphics
这是一个绘图专用的API族,它经常被称为QuartZ或QuartZ 2D。Core Graphics是iOS上所有绘图功能的基石,包括UIKit。


绘图方式
三种获得图形上下文的方法(drawRect:drawRect: inContext:UIGraphicsBeginImageContextWithOptions)。那么我们就有6种绘图的形式:

  1. drawRect:
- (void) drawRect: (CGRect) rect {
    // UIKit 方式
    UIBezierPath* p = [UIBezierPathbezierPathWithOvalInRect:CGRectMake(0,0,100,100)];
    [[UIColor blueColor] setFill];
    [p fill];
}
- (void) drawRect: (CGRect) rect {
    //  Core Graphics 方式
    CGContextRef con = UIGraphicsGetCurrentContext();
    CGContextAddEllipseInRect(con, CGRectMake(0,0,100,100));
    CGContextSetFillColorWithColor(con, [UIColor blueColor].CGColor);
    CGContextFillPath(con);
}
  1. drawRect: inContext:
- (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)ctx {
    // UIKit 方式
    UIGraphicsPushContext(ctx);
    UIBezierPath* p = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0,0,100,100)];
    [[UIColor blueColor] setFill];
    [p fill];
    UIGraphicsPopContext();
}
- (void)drawLayer:(CALayer*)lay inContext:(CGContextRef)con {
    //  Core Graphics 方式
    CGContextAddEllipseInRect(con, CGRectMake(0,0,100,100));
    CGContextSetFillColorWithColor(con, [UIColor blueColor].CGColor);
    CGContextFillPath(con);
}
  1. UIGraphicsBeginImageContextWithOptions
    // UIKit 方式
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(100,100), NO, 0);
    UIBezierPath* p = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0,0,100,100)];
    [[UIColor blueColor] setFill];    
    [p fill];
    UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    //  Core Graphics 方式
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(100,100), NO, 0);
    CGContextRef con = UIGraphicsGetCurrentContext();
    CGContextAddEllipseInRect(con, CGRectMake(0,0,100,100));
    CGContextSetFillColorWithColor(con, [UIColor blueColor].CGColor);
    CGContextFillPath(con);
    UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

总结
通过以上两种框架在三种上下文场景的绘制,总结出如下:

  1. UIKit 方式:在drawRect: inContext:场景绘制时,需先调用UIGraphicsPushContext(ctx);,结束时需调用UIGraphicsPopContext();。其余情况下直接绘制即可。
  2. Core Graphics 方式:只有在drawRect: inContext:时可以直接绘制,其余情况下需先调用UIGraphicsGetCurrentContext();,结束时需调用UIGraphicsEndImageContext();
  3. Core Graphics 方式绘制时接口都是:CGContextXXXX 这种,UIKit 方式绘制时都是
    [xxx fill][NSString Draw:][UIImage Draw:]这种。

绘制哪些呢
CGContext:表示一个图形环境;
CGPath:使用向量图形来创建路径,并能够填充和stroke;
CGImage:用来表示位图;
CGLayer:用来表示一个能够用于重复绘制和offscreen绘制的绘制层;
CGPattern:用来表示Pattern,用于重复绘制;
CGShading和 CGGradient:用于绘制剃度;
CGColor 和 CGColorSpace;用来进行颜色和颜色空间管理;
CGFont, 用于绘制文本;
CGPDFContentStream、CGPDFScanner、CGPDFPage、CGPDFObject,CGPDFStream, CGPDFString等用来进行pdf文件的创建、解析和显示。

其他:
Quartz 2D Programming Guide
Core Text Programming Guide
Text Programming Guide for iOS
OpenGL ES Programming Guide
Core Animation Programming Guide
Core Image Programming Guide
Drawing and Printing Guide for iOS

  1. https://www.jianshu.com/p/5e4f7567df4d
  2. https://www.jianshu.com/p/6350feff9f47
  3. https://www.jianshu.com/p/2ed248263cff
  4. https://www.jianshu.com/p/6810d8e1a5d0/

相关文章

  • iOS 绘图几种场景下的上下文CGContextRef

    上面的图可以看出iOS中绘图框架与其之前的关系; 作为初学者,很容易被UIKit和Core Graphics两个支...

  • UIGraphicsGetCurrentContext

    CGContextRef CGContextRef即图形上下文。可以这么理解,我们绘图是需要一个载体或者说输出目标...

  • Quartz2D的使用

    一、图形上下文 一个CGContextRef类型的属性,用于: 保存绘图信息、绘图状态 决定绘制的输出目标 类型:...

  • Core Graphics初步使用

    view:使用Core Graphics之前需要指定一个用于绘图的图形上下文(CGContextRef),这个图形...

  • iOS绘制涂鸦

    其实绘图过程也就点成线的过程,在ios绘图需要使用到的CGContextRef和CGMutablePath...

  • iOS-CGContextRef开启上下文绘图

    前面我们讲了UIBezierPath绘图,UIBezierPath只能画线,描绘几何图形。CGContextRef...

  • iOS CGContextRef/UIBezierPath(绘图

    绘图的底层实现方法 注意:在drawRect方法中系统会默认创建一个上下文(C语言类型)在其他方法中不会有这样一个...

  • 深入理解图形上下文

    深入理解图形上下文 CGContextRef 结构体,携带Quartz 2D引擎进行一次绘图操作所需的信息。这些信...

  • 绘制曲线方法

    //获取上下文 CGContextRef context = UIGraphicsGetCurrentCont...

  • iOS-绘图Quartz 2D 贝赛尔曲线相关

    本篇涵盖iOS中绘图上下文,截屏相关等. 1.玩转iOS中的绘图(Quartz 2D基础篇)2.分享iOS中常用的...

网友评论

    本文标题:iOS 绘图几种场景下的上下文CGContextRef

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