CGContext图形上下文详解

CGContext图形上下文详解

2023年7月13日发(作者:)

CGContext图形上下⽂详解核⼼绘图状态管理CGContextSaveGState函数的作⽤是将当前图形状态推⼊堆栈。之后,您对图形状态所做的修改会影响随后的描画操作,但不影响存储在堆栈中的拷贝。在修改完成后,您可以通过CGContextRestoreGState函数把堆栈顶部的状态弹出,返回到之前的图形状态。这种推⼊和弹出的⽅式是回到之前图形状态的快速⽅法,避免逐个撤消所有的状态修改;这也是将某些状态(⽐如裁剪路径)恢复到原有设置的唯⼀⽅式。CGContextSaveGState:压栈操作,保存⼀份当前图形上下⽂CGContextRestoreGState:出栈操作,恢复⼀份当前图形上下⽂图形上下⽂的坐标空间变换CTM(current transformation matrix当前转换矩阵)CGContextScaleCTM:坐标系X,Y缩放CGContextTranslateCTM:坐标系平移CGContextRotateCTM:坐标系旋转CGContextConcatCTM:CGContextGetCTM:获得⼀份CTM设置Line属性及连接样式CGContextSetLineWidth:CGContextSetLineCap:CGContextSetLineJoin:CGContextSetMiterLimit:CGContextSetLineDash:CGContextSetFlatness:CGContextSetAlpha://设置透明度CGContextSetAlpha(context, 0.2)

CGContextSetBlendMode:设置Path绘制CGContextBeginPath:CGContextMoveToPoint:画笔移动到该点开始画线CGContextAddLineToPoint:画直线到该点CGContextAddCurveToPoint:画三次曲线函数CGContextMoveToPoint(context, 200, 300);//设置Path的起点

CGContextAddCurveToPoint(context,250, 280, 250, 400, 280, 300);//设置贝塞尔曲线的控制点坐标和控制点坐标终点坐标CGContextStrokePath(context);CGContextAddQuadCurveToPoint:画⼆次曲线CGContextMoveToPoint(context, 120, 300);//设置Path的起点CGContextAddQuadCurveToPoint(context,190, 310, 120, 390);//设置贝塞尔曲线的控制点坐标和终点坐标CGContextStrokePath(context);CGContextClosePath:闭合曲线CGContextAddRect:画矩形CGContextAddRect(context,CGRectMake(140, 120, 60, 30));//画⽅框CGContextAddRects:CGContextAddLines:/*画三⾓形*/ //只要三个点就⾏跟画⼀条线⽅式⼀样,把三点连接起来 CGPoint sPoints[3];//坐标点 sPoints[0] =CGPointMake(100, 220);//坐标1 sPoints[1] =CGPointMake(130, 220);//坐标2 sPoints[2] =CGPointMake(130, 160);//坐标3 CGContextAddLines(context, sPoints, 3);//添加线 CGContextClosePath(context); CGContextDrawPath(context, kCGPathFillStroke); //根据坐标绘制路径CGContextAddEllipseInRect://画椭圆 CGContextAddEllipseInRect(context, CGRectMake(160, 180, 20, 8)); //椭圆 CGContextDrawPath(context, kCGPathFillStroke);CGContextAddArc:CGContextAddArcToPoint:/*画圆⾓矩形*/ float fw = 180; float fh = 280;

CGContextMoveToPoint(context, fw, fh-20); // 开始坐标右边开始 CGContextAddArcToPoint(context, fw, fh, fw-20, fh, 10); // 右下⾓⾓度 CGContextAddArcToPoint(context, 120, fh, 120, fh-20, 10); // 左下⾓⾓度 CGContextAddArcToPoint(context, 120, 250, fw-20, 250, 10); // 左上⾓ CGContextAddArcToPoint(context, fw, 250, fw, fh-20, 10); // 右上⾓ CGContextClosePath(context); CGContextDrawPath(context, kCGPathFillStroke); //根据坐标绘制路径CGContextAddPath:CGContextCopyPath:获取路径信息CGContextReplacePathWithStrokedPath:CGContextIsPathEmpty:表⽰⽬前的图形上下⽂是否包含任何的⼦路径。bool isPathEmpty = CGContextIsPathEmpty(ctf);CGContextGetPathCurrentPoint:返回⼀个⾮空的路径中的当前点。CGPoint currentPoint = CGContextGetPathCurrentPoint(ctf);// 获取当前画笔处于最后的那⼀个点CGContextGetPathBoundingBox:返回包含当前路径的最⼩矩形。 CGRect boxRect = CGContextGetPathBoundingBox(ctf);// 包含路径的最⼩矩形CGContextPathContainsPoint:检查当前路径中是否包含指定的点。路径绘制CGContextStrokePath:CGContextDrawPath:两者区别: /*CGPathDrawingMode是填充⽅式,枚举类型 kCGPathFill:只有填充(⾮零缠绕数填充),不绘制边框 kCGPathEOFill:奇偶规则填充(多条路径交叉时,奇数交叉填充,偶交叉不填充) kCGPathStroke:只有边框 kCGPathFillStroke:既有边框⼜有填充 kCGPathEOFillStroke:奇偶填充并绘制边框 */CGContextStrokePath(context); 直接在图形上下⽂中渲染路径CGContextDrawPath(context, kCGPathFillStroke); //指定模式下渲染路径CGContextFillPath://填充路径CGContextEOFillPath://奇偶填充CGContextFillRect://CGContextStrokeRect:/*画矩形*/ CGContextStrokeRect(context,CGRectMake(100, 120, 10, 10));//画⽅框CGContextFillRect(context,CGRectMake(120, 120, 10, 10));//填充框CGContextFillRects:CGContextStrokeRectWithWidth:CGContextClearRect:CGContextFillEllipseInRect:CGContextStrokeEllipseInRect:CGContextStrokeLineSegments:修改剪裁路径CGContextClip:CGContextEOClip:CGContextClipToMask:CGContextGetClipBoundingBox:CGContextClipToRect:CGContextClipToRects:设置颜⾊、⾊彩空间及阴影值Quartz 中的颜⾊是⽤⼀组数值来表⽰。⽽颜⾊空间⽤于解析这些颜⾊信息,常⽤颜⾊空间有 RGB 、CMYK等。Quartz ⽀持通⽤颜⾊空间、设备独⽴颜⾊空间、设备依赖颜⾊空间、索引颜⾊空间和模式(Pattern)颜⾊空间。iOS不⽀持设备独⽴颜⾊空间和通⽤颜⾊空间。iOS应⽤程序必须使⽤设备颜⾊空间。CGContextSetFillColorWithColor://设置填充颜⾊CGContextSetStrokeColorWithColor://设置描边颜⾊调⽤如下函数来便捷的使⽤ CGColor 设置颜⾊值并使⽤ CGColor 指定的颜⾊空间//设置填充颜⾊ UIColor *aColor = [UIColor blueColor];//blue蓝⾊ CGContextSetFillColorWithColor(context, r);//填充颜⾊//设置描边颜⾊aColor = [UIColor yellowColor];CGContextSetStrokeColorWithColor(context, r);//线框颜⾊创建设备依赖颜⾊空间Quartz 中的颜⾊是⽤⼀组数值来表⽰。⽽颜⾊空间⽤于解析这些颜⾊信息,常⽤颜⾊空间有 RGB 、CMYK等。Quartz ⽀持通⽤颜⾊空间、设备独⽴颜⾊空间、设备依赖颜⾊空间、索引颜⾊空间和模式(Pattern)颜⾊空间。iOS不⽀持设备独⽴颜⾊空间和通⽤颜⾊空间。iOS应⽤程序必须使⽤设备颜⾊空间。CGColorSpaceCreateDeviceGray() 创建设备依赖灰度颜⾊空间。CGColorSpaceCreateDeviceRGB() 创建设备依赖RGB颜⾊空间。CGColorSpaceCreateDeviceCMYK() 创建设备依赖CMYK颜⾊空间。CGContextSetFillColorSpace:CGContextSetStrokeColorSpace:CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();CGContextSetFillColorSpace(context, colorSpace);CGContextSetStrokeColorSpace(context, colorSpace);CGContextSetFillColor:CGContextSetStrokeColor://Device RGB.

CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);CGContextSetRGBFillColor(context, 1, 0, 0, 1);CGContextSetGrayFillColor:CGContextSetGrayStrokeColor:调⽤如下函数来便捷的设置设备依赖CMYK颜⾊空间并设置颜⾊值。//Device extSetGrayStrokeColor(context, 0.5, 1);CGContextSetGrayFillColor(context, 0.5, 1);CGContextSetRGBFillColor:CGContextSetRGBStrokeColor:CGContextSetRGBStrokeColor (context, 142.0/ 255.0, 161.0/ 255.0, 189.0/ 255.0, 1.0);CGContextSetCMYKFillColor:CGContextSetCMYKStrokeColor:调⽤如下函数来便捷的设置设备依赖CMYK颜⾊空间并设置颜⾊值。//Device CMYK.

CGContextSetCMYKStrokeColor(context, 1, 0, 0, 0, 1);CGContextSetCMYKFillColor(context, 1, 0, 0, 0, 1);CGContextSetRenderingIntent:设置再现意图每个设备都有固定的可复制的颜⾊范围(gamut),这是设备的物理性质决定的。当图像从⼀个颜⾊空间向另⼀个颜⾊空间转换时,有些源设备颜⾊空间中呈现的颜⾊,不能在⽬标设备颜⾊空间中复制出来,这些不能复制的颜⾊叫⾊域外(out-of-gamut)颜⾊。⽐如 RGB 颜⾊空间⽐ CMYK 的颜⾊空间要⼤,有些在显⽰器上能显⽰的颜⾊不能在打印机上同样打印出来。因为我们不能在⽬标设备颜⾊空间中复制出⾊域外颜⾊,我们必须⽤⼀些其他颜⾊来替代他们。颜⾊空间转换时颜⾊替换调整的规则就是再现意图。更详细的说明可以查看 这⾥ 和 这⾥再现意图⽤于指定如何将源颜⾊空间的颜⾊映射到图形上下⽂的⽬标颜⾊空间的颜⾊范围内。如果不显式的指定再现意图,Quartz 使⽤“相对⾊度再现意图”应⽤于所有绘制(不包含位图图像)。对于位图图像,Quartz默认使⽤“感知再现意图”。调⽤ CGContextSetRenderingIntent(context, kCGRenderingIntentDefault) 来设置再现意图。再现意图共有以下 5 种typedef CF_ENUM (int32_t, CGColorRenderingIntent) { kCGRenderingIntentDefault, kCGRenderingIntentAbsoluteColorimetric, kCGRenderingIntentRelativeColorimetric, kCGRenderingIntentPerceptual, kCGRenderingIntentSaturation};1 kCGRenderingIntentDefault:默认再现意图。2 kCGRenderingIntentAbsoluteColorimetric:绝对⾊度再现意图。将输出设备颜⾊域外的颜⾊映射为输出设备域内与之最接近的颜⾊。这可以产⽣⼀个裁减效果,因为⾊域外的两个不同的颜⾊值可能被映射为⾊域内的同⼀个颜⾊值。当图形使⽤的颜⾊值同时包含在源⾊域及⽬标⾊域内时,这种⽅法是最好的。常⽤于logo或者使⽤专⾊(spot color)时。3 kCGRenderingIntentRelativeColorimetric:相对⾊度再现意图。转换所有的颜⾊(包括⾊域内的),以补偿图形上下⽂的⽩点与输出设备⽩点之间的⾊差。4 kCGRenderingIntentPerceptual:感知再现意图。通过压缩图形上下⽂的⾊域来适应输出设备的⾊域,并保持源颜⾊空间的颜⾊之间的相对性。感知渲染意图适⽤于相⽚及其它复杂的⾼细度图⽚。5 kCGRenderingIntentSaturation:饱和度再现意图。把颜⾊转换到输出设备⾊域内时,保持颜⾊的相对饱和度。结果是包含亮度、饱和度颜⾊的图⽚。饱和度意图适⽤于⽣成低细度的图⽚,如描述性图表。CGContextDrawImage:在指定区域画图⽚/*图⽚*/ UIImage *image = [UIImage imageNamed:@""]; [image drawInRect:CGRectMake(60, 340, 20, 20)];//在坐标中画出图⽚ // [image drawAtPoint:CGPointMake(100, 340)];//保持图⽚⼤⼩在point点开始画图⽚,可以把注释去掉看看 CGContextDrawImage(context, CGRectMake(100, 340, 20, 20), e);CGContextDrawTiledImage:CGContextGetInterpolationQuality:返回当前的图形上下⽂的插值(插值(Interpolation)是在不天⽣像素的环境下增长图像像素⼤⼩的⼀种⽅法,在周围像素⾊彩的根蒂根基上⽤算术公式计算亡失像素的⾊彩。)质量⽔平CGContextSetInterpolationQuality:设置图形上下⽂的插值质量⽔平。设置阴影CGContextSetShadowWithColor:CGContextSetShadow:阴影是如何⼯作的Quartz中的阴影是图形状态的⼀部分。我们可以调⽤函数CGContextSetShadow来创建,并传⼊⼀个图形上下⽂、偏移值及模糊值。阴影被设置后,任何绘制的对象都有⼀个阴影,且该阴影在设备RGB颜⾊空间中呈现出⿊⾊的且alpha值为1/3。换句话说,阴影是⽤RGBA值{0, 0, 0, 1.0/3.0}设置的。我们可以调⽤函数CGContextSetShadowWithColor来设置彩⾊阴影,并传递⼀个图形上下⽂、 偏移值、模糊值有CGColor颜⾊对象。颜⾊值依赖于颜⾊空间。如何在调⽤CGContextSetShadow或CGContextSetShadowWithColor之前保存了图形状态,我们可以通过恢复图形状态来关闭阴影。我们也可以通过设置阴影颜⾊为NULL来关闭阴影阴影有三个属性:x偏移值,⽤于指定阴影相对于图⽚在⽔平⽅向上的偏移值。y偏移值,⽤于指定阴影相对于图⽚在竖直⽅向上的偏移值。模糊(blur)值,⽤于指定图像是有⼀个硬边CGContextSetShadow(context, CGSizeMake(10, -20), 10);绘制渐变⾊CGContextDrawLinearGradient:CGContextDrawRadialGradient: //第⼆种填充⽅式 CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB(); CGFloat colors[] = { 1,1,1, 1.00, 1,1,0, 1.00, 1,0,0, 1.00, 1,0,1, 1.00, 0,1,1, 1.00, 0,1,0, 1.00, 0,0,1, 1.00, 0,0,0, 1.00, }; CGGradientRef gradient = CGGradientCreateWithColorComponents (rgb, colors, NULL, sizeof(colors)/(sizeof(colors[0])*4));//形成梯形,渐变的效果 CGColorSpaceRelease(rgb); //画线形成⼀个矩形 //CGContextSaveGState与CGContextRestoreGState的作⽤ /* CGContextSaveGState函数的作⽤是将当前图形状态推⼊堆栈。之后,您对图形状态所做的修改会影响随后的描画操作,但不影响存储在堆栈中的拷贝。在修改完成后,您可 */ CGContextSaveGState(context); CGContextMoveToPoint(context, 220, 90); CGContextAddLineToPoint(context, 240, 90); CGContextAddLineToPoint(context, 240, 110); CGContextAddLineToPoint(context, 220, 110); CGContextClip(context);//context裁剪路径,后续操作的路径 //CGContextDrawLinearGradient(CGContextRef context,CGGradientRef gradient, CGPoint startPoint, CGPoint endPoint,CGGradientDrawingOptions options) //gradient渐变颜⾊,startPoint开始渐变的起始位置,endPoint结束坐标,options开始坐标之前or开始之后开始渐变 CGContextDrawLinearGradient(context, gradient,CGPointMake (220,90) ,CGPointMake(240,110), kCGGradientDrawsAfterEndLocation); CGContextRestoreGState(context);// 恢复到之前的context

//再写⼀个看看效果 CGContextSaveGState(context); CGContextMoveToPoint(context, 260, 90); CGContextAddLineToPoint(context, 280, 90); CGContextAddLineToPoint(context, 280, 190); CGContextAddLineToPoint(context, 260, 190); CGContextClip(context);//裁剪路径 //说⽩了,开始坐标和结束坐标是控制渐变的⽅向和形状 CGContextDrawLinearGradient(context, gradient,CGPointMake (260, 90) ,CGPointMake(260, 190), kCGGradientDrawsAfterEndLocation); CGContextRestoreGState(context);// 恢复到之前的context

//下⾯再看⼀个颜⾊渐变的圆 //参数1:图形上下⽂ //参数2:渐变⾊ //参数3:开始中⼼点 //参数4:开始半径 //参数5:结束中⼼点 //参数6:结束半径 //参数7:渲染模式 CGContextDrawRadialGradient(context, gradient, CGPointMake(400, 100), 0.0, CGPointMake(350, 100), 30, kCGGradientDrawsBeforeStartLocation);CGContextDrawShading:绘制⽂本CGContextSetCharacterSpacing:CGContextSetTextPosition:CGContextGetTextPosition:CGContextSetTextMatrix:CGContextGetTextMatrix:CGContextSetTextDrawingMode:CGContextSetFont:CGContextSetFontSize:CGContextShowGlyphsAtPositions:PDF** CGContextDrawPDFPage**: 绘制⼀个PDF页⾯到当前的⽤户空间建⽴⼀个基于页⾯的图形上下⽂CGContextBeginPage:CGContextEndPage:管理图形上下⽂CGContextRetain:CGContextRelease:CGContextSynchronize:锯齿功能CGContextSetShouldAntialias:设置图形上下⽂的抗锯齿开启或关闭。CGContextSetAllowsAntialiasing:字体展⽰功能CGContextSetShouldSmoothFonts:CGContextSetAllowsFontSmoothing:CGContextSetShouldSubpixelPositionFonts:CGContextSetAllowsFontSubpixelPositioning:CGContextSetShouldSubpixelQuantizeFonts:CGContextSetAllowsFontSubpixelQuantization:使⽤透明图层Quartz的透明层类似于许多流⾏的图形应⽤中的层。层是独⽴的实体。Quartz维护为每个上下⽂维护⼀个透明层栈,并且透明层是可以嵌套的。但由于层通常是栈的⼀部分,所以我们不能单独操作它们。我们通过调⽤函数CGContextBeginTransparencyLayer来开始⼀个透明层,该函数需要两个参数:图形上下⽂与CFDictionary对象。字典中包含我们所提供的指定层额外信息的选项,但由于Quartz 2D API中没有使⽤字典,所以我们传递⼀个NULL。在调⽤这个函数后,图形状态参数保持不变,除了alpha值[默认设置为1]、阴影[默认关闭]、混合模式[默认设置为normal]、及其它影响最终组合的参数。在开始透明层操作后,我们可以绘制任何想显⽰在层上的对象。指定上下⽂中的绘制操作将被当成⼀个组合对象绘制到⼀个透明背景上。这个背景被当作⼀个独⽴于图形上下⽂的⽬标缓存。当绘制完成后,我们调⽤函数CGContextEndTransparencyLayer。Quartz将结合对象放⼊上下⽂,并使⽤上下⽂的全局alpha值、阴影状态及裁减区域作⽤于组合对象。在透明层中绘制需要三步:1. 调⽤函数CGContextBeginTransparencyLayer2. 在透明层中绘制需要组合的对象3. 调⽤函数CGContextEndTransparencyLayerCGContextBeginTransparencyLayer:直到相应的调⽤CGContextEndTransparencyLayer,在指定范围内的所有后续绘制操作组合到⼀个完全透明的背景(它被视为⼀个单独的⽬标缓冲区从上下⽂)CGContextBeginTransparencyLayerWithRect:CGContextEndTransparencyLayer://代码⽰例: CGContextBeginTransparencyLayer(context, NULL);

CGFloat wd = 300; CGFloat ht = 300; CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor); CGContextFillRect(context, CGRectMake (wd/3 + 50, ht/2, wd/4, ht/4)); CGContextEndTransparencyLayer(context);⽤户空间与设备空间互换CGContextGetUserSpaceToDeviceSpaceTransform:CGContextConvertPointToDeviceSpace:CGContextConvertPointToUserSpace:CGContextConvertSizeToDeviceSpace:CGContextConvertSizeToUserSpace:CGContextConvertRectToDeviceSpace:CGContextConvertRectToUserSpace:

发布者:admin,转转请注明出处:http://www.yc00.com/news/1689263890a226395.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信