iOS开发那点儿事

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

2022-07-05  本文已影响0人  d4d5907268a9
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/
上一篇下一篇

猜你喜欢

热点阅读