CGContext 和 CIContext brave

CGContext

属于Core Graphics(使用Quartz 进行2D渲染,处理基于路径的绘图、抗锯齿渲染、渐变、图像、颜色管理、pdf文档等。 说白了就是2D绘图 渲染功能)框架.
我们平时用的其实都是他的引用类型 CGContextRef

   typedef struct CGContext *CGContextRef;

同时也发现,CGContext是一个结构体,而并不是一个Object。CGContextRef是一个指向CGContext结构体的指针。

其实 Core Graphics 其中的类型,都和这个类似。如:
CGColor  --->   CGColorRef
CGPath   --->   CGPathRef
CGImage  --->   CGImageRef
CGLayer  --->   CGLayerRef
CGShading--->   CGShadingRef
CGFont   --->   CGFontRef
CGPDFObject->   CGPDFObjectRef

CGContext 可以看做是一个2D 作图的画布。 而iOS 有多种画布,这些画布都以堆栈的形式存放。(只有最上面的那个context才有效,有push pop的概念)

  1. 获取CGContextRef
1. UIGraphicsGetCurrentContext () 
    在 -(void)drawRect:(CGRect)rect 里面获取contex的方法。

2. UIGraphicsPushContext(ctx)
    UIGraphicsPopContext()
    也就是压入栈 和 推出栈,
    在 -(void)drawLayer:(CALayer*)layer inContext:(CGContextRef)ctx
    里面调用即可吧当前的context 放到栈顶,方能生效。

    //该函数会自动创建一个context,并把它push到上下文栈顶
    //开始 scale 为0 则表示跟随设备,opaque YES黑色画布,NO透明画布
3. UIGraphicsBeginImageContextWithOptions(size, opaque, scale)
    UIGraphicsBeginImageContext(size)//开始
    UIGraphicsGetCurrentContext()//获取context
    UIGraphicsGetImageFromCurrentImageContext()  //获取图片
    UIGraphicsEndImageContext()  //结束
  1. 在Context上画
1. image drawInRect
2. layer renderInContext
3. CGContextSet...  //color, width, line, render, blendMode,  should...
    CGContextAdd...  //Line, Path, Arc, Ellipse, 
    CGContextDraw/Fill ...  //rect, path, stroke, replace

4. CGContextDrawImage(ctx, rect, image)
5. CGContextDrawShading(ctx, shading)
6. CGContextDrawLinearGradient(ctx, gradient, startPoint, endPoint, options); //渐变
7. CGContextDrawPDFPage(ctx, PDFPage);
8. CGContextShowGlyphsAtPositions(ctx, glyphs, *Lpositions, count);//雕文
9.CGContextShowText(ctx,  *string, length)//已作废,用CoreText 实现
  1. 坐标互转: Converting(基本上看,名字都能明白是什么意思)
CGContextGetUserSpaceToDeviceSpaceTransform //Transform

CGContextConvertPointToDeviceSpace
CGContextConvertPointToUserSpace

CGContextConvertSizeToDeviceSpace 
CGContextConvertSizeToUserSpace 

CGContextConvertRectToDeviceSpace 
CGContextConvertRectToUserSpace
  1. CTM (current graphics state's transformation matrix 当前图形状态的转换矩阵)
//在上下文中得到的CTM是:CTMnew = transform * CTMcontext。
CGContextConcatCTM(ctx, transform) //矩阵 相乘(顺序固定的)

 //在上下文中得到CTM
CGContextGetCTM(ctx)

CGContextRotateCTM     //旋转
CGContextTranslateCTM  //位移
CGContextScaleCTM      //缩放
  1. 保存,重设 context的状态
CGContextSaveGState(ctx)   保存当前的context的状态到堆栈上
CGContextRestoreGState(ctx)  把堆栈上第一个contex的保存状态,重新设置回去

CGContextGetTypeID  上下文的标识ID
CGContextFlush      强制吧所有的draw 都更新到contex(一般是系统周期性的自动调用 更新画布)
CGContextSynchronize更新画布,系统在下次刷新时,draw所有操作。(相当于 做了一个需要更新的标记)

CIContext

CIContext 属于Core Image框架(文档中提到 主要的功能就是 用内置或自定义的过滤器处理图片和视频 以及 在视频图片中检测面部和眼睛子类的特征和跟踪面部。和Core Graphics的主要区别 就是更注重于视频图片的加工处理)的,是一个OC对象。一般和 CIImage,CIColor,CIFilter等交互。

  1. 创建CIContext:
1. + contextWithOptions   / + context
2. + contextWithCGContext:options:     //CPU 渲染(用到了CGContextRef)
3. + contextWithCGLContext:pixelFormat:colorSpace:options: //GPU渲染(OpenGL 包含GL的)
  1. 在画布上画:
//获取 渲染的图片(CIImage -> CGImage)
 creatCGImage:fromRect ...
render:to ...    //Bitmap, CVPixelBuffer, IOSurface, MTLTexture(一个支持GPU加速3D绘图的API)

//在context上画 图片
- drawImage:inRect:fromRect:  

二者的关联, 使用:

    CIImage * outputImage = [filter outputImage]; //从CIFilter 获取CIImage
    CGRect qrRect = [colorImage extent];

    //设置CIContext,并从CIImage -> CGImage -> UIImage
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef cgImage = [context createCGImage: outputImage fromRect:qrRect];
    UIImage *resultIamge = [UIImage imageWithCGImage:cgImage];
    //(如果 直接用[UIImage imageWithCIImage:outputImage]; 会得到一个不是位图的图片)
    
    
    UIImage* deviceImage = [UIImage imageNamed:@"device"];
    
    //开始Image的CGContext
    UIGraphicsBeginImageContext(CGSizeMake(1000, 1000));
    
    //获取 image的 CGContextRef
    CGContextRef cgContext = UIGraphicsGetCurrentContext();

    //CGContextRef 和 CIContext 关联(二者表示同一画布)
    context = [CIContext contextWithCGContext:cgContext options:nil];
    
    CIImage* deviceCIImage = [CIImage imageWithCGImage:deviceImage.CGImage];
    CIImage* resultCIImage = [CIImage imageWithCGImage:resultIamge.CGImage];
    
    //CIContext 吧图片画在context上
    [context drawImage:deviceCIImage inRect:CGRectMake(0, 0, 1000, 1000) fromRect:[deviceCIImage extent]];
    [context drawImage:resultCIImage inRect:CGRectMake(200, 200, 600, 600) fromRect:qrRect];
    
    //从CGContextRef 上获取图片
    resultIamge = UIGraphicsGetImageFromCurrentImageContext();

    //结束Image的CGContext
    UIGraphicsEndImageContext();

从上面,可以发现。这两个context 都是画布的意思,相似的地方非常多,而且可以互相关联。只是所属的库不同,在用法上有些区别。

还有一个,就是CIImage 只有经过context 转化为CGImage后,才能变成位图图片。(非位图图片,不能保存到相册,不能转换为NSData (jpeg png))



作者:夜间寻路人
链接:https://www.jianshu.com/p/b43f42a7e494
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
原文地址:https://www.cnblogs.com/Free-Thinker/p/15740363.html