0 CGContextRef context = UIGraphicsGetCurrentContext(); 设置上下文 1 CGContextMoveToPoint 开始画线 2 CGContextAddLineToPoint 画直线 4 CGContextAddEllipseInRec t 画一椭圆 4 CGContextSetLineCap设置线条终点形状 4 CGContextSetLineDash 画虚线 4 CGContextAddRect 画一方框 4 CGContextStrokeRect指定矩形 4 CGContextStrokeRectWithW idth 指定矩形线宽度 4 CGContextStrokeLineSegme nts 一些直线 5CGContextAddArc 画已曲线 前俩店为中心 中间俩店为起始弧度 最后一数据为0则顺时针画 1则逆时针 5CGContextAddArcToPoint(context,0,0, 2, 9, 40);//先画俩条线从point 到 弟1点 , 从弟1点到弟2点的线 切割里面的圆 6 CGContextSetShadowWithCo lor 设置阴影 7 CGContextSetRGBFillColor 这只填充颜色 7CGContextSetRGBStrokeCol or 画笔颜色设置 7 CGContextSetFillColorSpa ce 颜色空间填充 7CGConextSetStrokeColorSp ace 颜色空间画笔设置 8 CGContextFillRect 补充当前填充颜色的rect 8CGContextSetAlaha 透明度 9 CGContextTranslateCTM 改变画布位置 10 CGContextSetLineWidth 设置线的宽度 11 CGContextAddRects 画多个线 12 CGContextAddQuadCurveToP oint 画曲线 13 CGContextStrokePath开始绘制图片 13 CGContextDrawPath 设置绘制模式 14 CGContextClosePath 封闭当前线路 15CGContextTranslateCTM(context, 0, rect.size.height); CGContextScaleCTM(context, 1.0, -1.0);反转画布 16 CGContextSetInterpolatio nQuality 背景内置颜色质量等级 16 CGImageCreateWithImageIn Rect从原图片中取小图 17 字符串的 写入可用 nsstring本身的画图方法 - (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font lineBreakMode:(UILineBreakMode)lineBreakMode alignment:(UITextAlignment)alignment;来写进去即可 18对图片放大缩小的功能就是慢了点 UIGraphicsBeginImageCont ext(newSize); UIImage* newImage = UIGraphicsGetImageFromCu rrentImageContext();UIGraphicsEndImageContex t(); 19 CGColorGetComponents() 返回颜色的各个直 以及透明度 可用只读constfloat 来接收 是个数组 20 画图片 CGImageRef image=CGImageRetain(img.CGImage);CGContextDrawImage(context, CGRectMake(10.0, height - 100.0, 90.0, 90.0), image); 21 实现逐变颜色填充方法 CGContextClip(context); CGColorSpaceRef rgb = CGColorSpaceCreateDevice RGB(); CGFloatcolors[] = { 204.0 / 255.0, 224.0 / 255.0, 244.0 / 255.0, 1.00, 29.0 / 255.0, 156.0 / 255.0,215.0 / 255.0, 1.00, 0.0 / 255.0, 50.0 / 255.0, 126.0 / 255.0, 1.00, }; CGGradientRefgradient = CGGradientCreateWithColo rComponents (rgb, colors, NULL,sizeof(colors)/(sizeof(colors[0])*4)); CGColorSpaceRelease(rgb); CGContextDrawLinearGradi ent(context, gradient,CGPointMake (0.0,0.0) ,CGPointMake(0.0,self.frame.size.height),kCGGradientDrawsBeforeSt artLocation); 22 注: 画完图后,必须 先用CGContextStrokePath来描线,即形状 后用CGContextFillPath来填充形状内的颜色. 填充一个路径的时候,路径里面的子路径都是独立填充的。 假如是重叠的路径,决定一个点是否被填充,有两种规则 1,nonzero winding number rule:非零绕数规则,假如一个点被从左到右跨过,计数器+1,从右到左跨过,计数器-1,最后,如果结果是0,那么不填充,如果是非零,那么填充。 2,even-odd rule: 奇偶规则,假如一个点被跨过,那么+1,最后是奇数,那么要被填充,偶数则不填充,和方向没有关系。Function Description CGContextEOFillPath 使用奇偶规则填充当前路径 CGContextFillPath 使用非零绕数规则填充当前路径 CGContextFillRect 填充指定的矩形 CGContextFillRects 填充指定的一些矩形CGContextFillEllipseInRe ct 填充指定矩形中的椭圆 CGContextDrawPath 两个参数决定填充规则,kCGPathFill表示用非零绕数规则,kCGPathEOFill表示用奇偶规则,kCGPathFillStroke表示填充,kCGPathEOFillStroke表示描线,不是填充 设置当一个颜色覆盖上另外一个颜色,两个颜色怎么混合 默认方式是 result = (alpha * foreground)+ (1 - alpha) * background CGContextSetBlendMode :设置blend mode. CGContextSaveGState :保存blend mode. CGContextRestoreGState:在没有保存之前,用这个函数还原blend mode.CGContextSetBlendMode 混合俩种颜色
1. 用两种方法创建渐变效果:CGShadingRef, CGGradientRef。
2. axial gradient:线性渐变, radical gradient 径向渐变
3. 可以只对透明度渐变。
4.CGShadingRef, 需要提供自定义的渐变函数。 CGGradientRef,需要提供自定义的线性渐变颜色数组。
5.使用CGGradient: 创建对象,使用CGContextDrawLinearGradi
6. 使用CGShading: 创建对象,创建函数,设置绘制状态,绘制对象,释放对象。
1. 阴影属性:x偏移,y偏移,模糊程度
2. CGContextSetShadow,阴影颜色(0,0,0,1/3),
CGContextSetShadowWithCo
3. 属于GraphicsState
1. 模板:一系列绘制操作的集合,可以和颜色一样使用。屏幕会被分为模板网格,每个网格绘制一次模板。
2. 模板网格需要定义边界,网格在边界内绘制。
3. 模板网格间可以定义间隔大小,可以使用模板矩阵定义模板空间。
4. 颜色模板:在模板中定义颜色。
stencil模板:在模板中定义mask。
5. 拉伸:不拉伸/最小变形/常数间隔
6. CGContextFillPattern
7. 绘制颜色模板:
- 自定义绘制函数。
- 创建模板颜色空间。
- 创建颜色模板对象。
- 设置颜色模板对象。
8. 绘制stencil模板对象:同上。
1.Quartz支持两种坐标系统:
- 用户坐标空间:用于文档页面的坐标系统,使用浮点数标记,与分辨率无关。
- 设备坐标空间:显示设备的坐标系统,使用整数标记,与分辨率相关。
Quartz自动将用户坐标空间匹配到设备坐标空间。
2. CTM进行坐标变换,创建Context时CTM是单位阵。
3. Rotation,Scaling,Translation
4. 创建CGAffineTransform,并应用到CTM上
5. CGAffineTransformEqualTo
6. CGContextGetUserSpaceToD
CGContextConverPointToDe
CGContextConverSizeToDev
CGContextConvertRectToDe
1. 三色:RGB或者CMY
2. 视觉由视锥细胞和视杆细胞产生。视锥细胞感应亮度,视杆细胞会对蓝色和红色产生感应,默认对黄色产生感应。
3. HSV:Hue,Saturation,Value
4. 屏幕使用颜色相加机制,打印机使用颜色相减机制。
5. 颜色空间:
- 灰度空间:0黑,0.5灰,1.0白
- RGB空间:R G B C M Y K W, sRGB:对RGB的矫正,增加在色温为6500度时做2.2Gamma矫正。
- HSV,HSL
- CMY,CMYK:C = 1- R, M= 1-G, Y= 1-B
- XYZ:CIE based color space。独立于显示设备的颜色空间。所有CIE颜色空间都基于XYZ颜色空间。
XYZ分别代表三种视觉感知细胞对光源的感知程度,由大量的实验统计得出,Y为光强。XYZ为非线性空间。
Yxy 空间:Y =Y, x = X/(X+Y+Z), y = Y/(X+Y+Z)
- LUV空间:对XYZ空间的非线性校正,定义了某个颜色到一个参考白点的距离。不是设备独立的颜色空间。用于比较相似的颜色。
- 索引颜色空间:调色板。
- 名字颜色空间:每种颜色有对应的名字。
6. 颜色管理系统:
- 颜色特性集合。
- 颜色管理模块。
- 颜色匹配系统。
7. 颜色在不同空间转换时会造成颜色丢失。
8. 颜色空间描述:最亮点,最暗点,与目标设备的区别,RGBCMY的最大强度等。创建图像设备的描述叫Source Profile,显示图像设备的描述叫Destination Profile。
9. Profile Connection Space:将一个设备独立颜色空间的颜色转换为设备依赖颜色空间的颜色,使用颜色空间描述转换。
10. 描述类型:
- 设备描述:描述一个设备,包括输入,显示和输出设备。输入包括扫描仪和相机,输出包括打印机和电影拷贝,显示包括显示器和LCD。
- 颜色空间描述:
- 抽象描述:用户自定义的颜色空间描述。
- 设备连接描述:直接描述设备间颜色转换的描述。
- 命名颜色空间的描述:
颜色空间描述能够被嵌入到图像中。
11. 绘制目的:CGContextSetRenderingInt
- 视觉匹配:不同设备输出在视觉上保持一致。 kCGRenderingIntentPercep
- 相对色阶匹配:kCGRenderingIntentRelati
- 饱和度匹配:kCGRenderingIntentSatura
- 绝对色阶匹配:kCGRenderingIntentAbsolu
12. ColorSync:苹果的设备独立颜色管理系统。
13. Alpha值:半透明。CGContextSetAlpha,CGContextClearRect
14. Quartz支持创建设备独立颜色空间。iOS只支持设备相关颜色空间。
CGColorSpaceCreateLab/CGColorSpaceCreateICCBas
CGColorSpaceGenericGray/CGColorSpaceGenericRGB/CGColorSpaceGenericCMYK
CGColorSpaceCreateDevice
CGColorSpaceCreateIndexe
15. Quartz中某个颜色必须和某个颜色空间相关。
CGContextSetFillColorSpa
CGContextSetRGBFillColor
CGContextSetCMYKFillColo
CGContextSetGrayFillColo
CGContextSetFillColorWit
CGContextSetFillColor/CGContextSetStrokeColor (current color space)
16. 创建颜色:CGColorCreate
1. 向量绘制,用路径来描述图形,可以是闭合也可以不是闭合。
2. Building Blocks:
- 点: CGContextMoveToPoint
-线: CGContextAddLineToPoint, CGContextAddLines
-圆弧:CGContextAddArc,CGContextAddArcToPoint
-曲线:Quadratic/Cubic Bezier曲线, CGContextAddCurveToPoint
- CGContextClosePath会被某些操作默认执行。
- 椭圆:CGContextAddEllipseInRec
- 矩形: CGContextAddRect;
3. 创建Path CGContextBeginPath + CGContextMoveToPoint
4. Painting Path != Create Path
5. Mutable Path: Path对象,独立于Context存在。CGContextAddPath来使用它。
- CGPathCreateMutable = CGContextBeginPath
- CGPathMoveToPoint = CGContextMoveToPoint
- CGPathAddLineToPoint = CGContextAddLineToPoint
- CGPathAddCurveToPoint = CGContextAddCurveToPoint
- CGPathAddEllipseInRect = CGContextAddEllipseInRec
- CGPathAddArc = CGContextAddArc
- CGPathAddRect = CGContextAddRect
- CGPathCloseSubPath = CGContextCloseSubPath
6. 描边
- 线宽:
- 连接方式:Miter尖角,Round圆角,Bevel平角
- 线头:Butt平头,Round圆头,Projecting扩展平头
- 角限:限制尖角连接的范围
- 点划模板:
- 颜色空间:
- 颜色:
- StrokePattern?
CGContextStrokePath/CGContextStrokeRect/CGContextStrokeRectWithW
7.填充规则:
- nonzero winding:CGContextFillPath从某点出发向图形边缘做一条射线,如果射线和图形某条边相交,且该边从坐向右穿过射线,则相交计数+1,如果该边从右向左穿过射线,则相交计数-1。如果最后相交计数为1,则该点在图形内。
- even odd:CGContextEOFillPath从某点出发向图形边缘做一条射线,如果射线和图形边相交点数为奇数,则该点在图形内。
8. CGContextFillPath/CGContextEOFillPath/CGContextFillRect/CGContextFillRects/CGContextFillEllipseInRe
9. 混合:CGContextSetBlendMode – GraphicsState, 通常:
- Normal: result
- Multiply: result = fore*back;
- Screen: result = 1.0-(1.0-fore)*(1.0-back);
- Overlay: result = gray(back)>0.5?(1.0-2.0*(1.0-back)*(1.0-fore):fore*back*2.0f;
- Darken: result = min(fore,back);
- Lighten: result = max(fore,back);
- Color Dodge: result = back/(1.0-fore);
- Color Burn: result = 1.0 – (1.0-back)/fore;
- Soft Light: result = gray(fore)>0.5? 1.0 – (1.0-back)*(1.5 – fore):back*(fore+0.5);
- Hard Light: result = gray(fore)>0.5?1.0 – 2.0*(1.0-back)*(1.0-fore):2.0*back*fore;
- Difference: result = abs(fore-back);
- Exclusion: result = 0.5 – 2.0*(fore – 0.5)*(back-0.5);
- Hue: result = lum(back), sat(back),hue(fore);
- Saturtation: result = lum(back),sat(fore),hue(back);
- Color: result = lum(back),sat(fore),hue(fore);
- Luminosity: result = lum(fore),sat(back),hue(back);
10.裁剪: CGConextClip/CGContextEOClip/CGContextClipToRect/CGContextClipToRects/CGContextClipToMask;
1. CGContext类。
2. UIView::DrawRect函数
3. UIGraphicsGetCurrentCont
4. UIView坐标系和Quartz坐标系相反。
5. CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort];
6. initWithFrame, intWithCoder, DrawRect
7. CGPDFContextCreateWithUR
CFURLCreateWithFileSyste
8. iOS 推荐使用 UIGraphicsBeginImageCont
9. CGBitmapContextCreate
10. Pixel Format
NULL color space:
- kCGImageAlphaoOnly 8bpp = A8
Gray color space:
- kCGImageAlphaNone 8bpp = L8
- kCGImageAlphaOnly 8bpp = A8
- kCGImageAlphaNone 16bpp = L16
- KCGImageAlphaNone|kCGBitmapfloatComponents
RGB color space:
- kCGImageAlphaNoneSkipFir
- kCGImageAlphaNoneSkipFir
- kCGImageAlphaNoneSkipLas
- kCGImageAlphaPremultipli
- kCGImageAlphaPremultipli
- kCGImageAlphaPremultiple
- kCGImageAlphaNonSkipLast
- kCGImageAlphaNoneSkipLas
- kCGImageAlphaPremultipli
CMYK space:
- kCGImageAlphaNone 32bpp, 8bpc = C8M8Y8K8
- kCGImageAlphaNone 64bpp, 16bpx = C16M16Y16K16
- kCGImageAlphaNone|kCGBitmapFloatComponents
11. CGContextSetShouldAntial
1、iOS支持OpenGL ES和Quartz/UIKit/CoreAnimation绘制接口。UIKit绘制必须在主线程中完成。
2、Quartz支持基于路径的绘制,反走样,填充,图像,上色,坐标变换,pdf绘制显示解析等功能。
3、UIKit支持线条绘制、图像和颜色操作。
4、Core Animation支持动画绘制。
5、View的使用DrawRect绘制,以下行为会触发:
- View的移动和遮挡。
- View的隐藏和显示。
- 拖动View。
- 显示调用setNeedDisplay和setNeedDispalyRect
6、UIKit左上角为原点,右下角为终点。CoreAnimation坐下角为原点,右上角为终点。使用CGContextRotateCTM、CGContextScaleCTM、CGContextTranslateCTM来变换矩阵,或者直接使用CGAffineTransform设置变换矩阵。
7、CGContext绘制上下文,对于Bitmap和PDF,可以创建不同的context类型。
- 变换矩阵
- 裁剪范围
- 线条绘制属性
- 曲线精度
- 反走样
- 填充属性,描边属性
- 半透明属性
- 颜色空间
- 文字
- 颜色混合模式
8、使用UIGraphicsGetCurrentCont
9、UIGraphicsBeginImageCont
UIGraphicsBeginPDFContex
10、Path绘制,即向量绘制。推荐使用UIBezierPath,其次是CGPath。
11、翻转屏幕变换:
CGContextTranslateCTM(graphicsContext, 0.0, drawingRect.size.height); |
CGContextScaleCTM(graphicsContext, 1.0, -1.0); |
12、Point通常等于Pixel,但是可以指定一个Point对应多个Pixel。
13、使用UIColor坐颜色空间变换。
14、绘制性能:
- 最小化绘制调用
- 尽量使用不透明的View
- 在卷屏时重用View和表格
- 在卷屏时可以不清空上次绘制结果
- 减少绘制状态切换。