您的位置:首页 > 移动开发 > IOS开发

iOS -- 图形绘制(全)

2015-12-23 17:42 507 查看
画阴影:

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor grayColor].CGColor);
CGContextFillRect(context, imageRect);

CGContextSetShadowWithColor(context, CGSizeMake(20, 20), 20, [UIColor blackColor].CGColor);
//设置阴影后,绘制的所有图形都带有阴影
[image drawInRect:imageRect blendMode:kCGBlendModeLuminosity alpha:1.0f];
//关闭阴影
CGContextSetShadow(context, CGSizeZero, 0);
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextFillRect(context, CGRectMake(10, 10, 20, 20));

CGContextRelease(context);

Paths

1. 向量绘制,用路径来描述图形,可以是闭合也可以不是闭合。

CGContextRef ctx = UIGraphicsGetCurrentContext();


CGContextRef
ctx
= [[NSGraphicsContext currentContext] graphicsPort];

CGMutablePathRef path = CGPathCreateMutable();


2. Building Blocks:

- 点: CGContextMoveToPoint

- 线: CGContextAddLineToPoint, CGContextAddLines

- 圆弧:CGContextAddArc,CGContextAddArcToPoint

- 曲线:Quadratic/Cubic Bezier曲线, CGContextAddCurveToPoint, CGContextAddQuadCurveToPoint

- CGContextClosePath会被某些操作默认执行。

- 椭圆:CGContextAddEllipseInRect;

- 矩形: 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 = CGContextAddEllipseInRect

- CGPathAddArc = CGContextAddArc

- CGPathAddRect = CGContextAddRect

- CGPathCloseSubPath = CGContextCloseSubPath

6. 描边

- 线宽:

- 连接方式:Miter尖角,Round圆角,Bevel平角

- 线头:Butt平头,Round圆头,Projecting扩展平头

- 角限:限制尖角连接的范围

- 点划模板:

- 颜色空间:

- 颜色:

- StrokePattern?

CGContextStrokePath/CGContextStrokeRect/CGContextStrokeRectWithWidth/CGContextStrokeEllipseInRect/CGContextStrokeLineSegment/CGContextDrawPath

7.填充规则:

- nonzero winding:CGContextFillPath从某点出发向图形边缘做一条射线,如果射线和图形某条边相交,且该边从坐向右穿过射线,则相交计数+1,如果该边从右向左穿过射线,则相交计数-1。如果最后相交计数为1,则该点在图形内。

- even odd:CGContextEOFillPath从某点出发向图形边缘做一条射线,如果射线和图形边相交点数为奇数,则该点在图形内。

8. CGContextFillPath/CGContextEOFillPath/CGContextFillRect/CGContextFillRects/CGContextFillEllipseInRect/CGContextDrawPath

9. 混合:CGContextSetBlendMode - GraphicsState, 通常:

- Normal: result = result = (alpha*fore) + (1.0-alpha)*back;

- 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;

IOS中的图形和绘制

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、使用UIGraphicsGetCurrentContext来获取当前的CGContext。

9、UIGraphicsBeginImageContextWithOptions和UIGraphicsEndImageContext用来包含图像绘制的代码。

UIGraphicsBeginPDFContextToFile(ToData)和UIGraphicsEndPDFContext用来包含PDF绘制的代码。

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和表格

- 在卷屏时可以不清空上次绘制结果

- 减少绘制状态切换。

CoreGraphics绘图整理一(转)

Points
void CGContextMoveToPoint (
CGContextRef c,
CGFloat x,
CGFloat y
);
指定一个点成为current point
Quartz会跟踪current point一般执行完一个相关函数后,current point都会相应的改变.

Lines
相关的几个函数
void CGContextAddLineToPoint (
CGContextRef c,
CGFloat x,
CGFloat y
);
创建一条直线,从current point到 (x,y)
然后current point会变成(x,y)
void CGContextAddLines (
CGContextRef c,
const CGPoint points[],
size_t count
);
创建多条直线,比如points有两个点,那么会画两条直线 从current point到 (x1,y1),
然后是(x1,y1)到(x2,y2)
然后current point会变成points中的最后一个点
Arcs
两种方法创建弧度 第一种
void CGContextAddArc (
CGContextRef c,
CGFloat x, //圆心的x坐标
CGFloat y, //圆心的x坐标
CGFloat radius, //圆的半径
CGFloat startAngle, //开始弧度
CGFloat endAngle, //结束弧度
int clockwise //0表示顺时针,1表示逆时针
);
假如想创建一个完整的圆圈,那么 开始弧度就是0 结束弧度是 2pi, 因为圆周长是 2*pi*r.
最后,函数执行完后,current point就被重置为(x,y).
还有一点要注意的是,假如当前path已经存在一个subpath,那么这个函数执行的另外一个效果是
会有一条直线,从current point到弧的起点

第二种
void CGContextAddArcToPoint (
CGContextRef c,
CGFloat x1, //端点1的x坐标
CGFloat y1, //端点1的y坐标
CGFloat x2, //端点2的x坐标
CGFloat y2, //端点2的y坐标
CGFloat radius //半径
);
原理:首先画两条线,这两条线分别是 current point to (x1,y1) 和(x1,y1) to (x2,y2).
这样就是出现一个以(x1,y1)为顶点的两条射线,
然后定义半径长度,这个半径是垂直于两条射线的,这样就能决定一个圆了,更好的理解看下图,不过个人认为下图所标的 tangent point 1的位置是错误的。
最后,函数执行完后,current point就被重置为(x2,y2).
还有一点要注意的是,假如当前path已经存在一个subpath,那么这个函数执行的另外一个效果是
会有一条直线,从current point到(x1,y1)

Curves
画曲线,一般是一条直线,然后定义几个控制点,使直线变弯曲。
三次曲线函数
void CGContextAddCurveToPoint (
CGContextRef c,
CGFloat cp1x, //控制点1 x坐标
CGFloat cp1y, //控制点1 y坐标
CGFloat cp2x, //控制点2 x坐标
CGFloat cp2y, //控制点2 y坐标
CGFloat x, //直线的终点 x坐标
CGFloat y //直线的终点 y坐标
);
假如第二个控制点(cp2x,cp2y)比(cp1x,cp1y) 更接近current point,那么会形成一个封闭的曲线

二次曲线函数
void CGContextAddQuadCurveToPoint (
CGContextRef c,
CGFloat cpx, //控制点 x坐标
CGFloat cpy, //控制点 y坐标
CGFloat x, //直线的终点 x坐标
CGFloat y //直线的终点 y坐标
);
执行完函数貌似current point不会变化,没有具体测试过

Ellipses

void CGContextAddEllipseInRect (

CGContextRef context,

CGRect rect //一矩形

);

如果矩形是一个正方形,那么画出来就是一个圆

执行完函数貌似current point不会变化,没有具体测试过

Rectangles

void CGContextAddRect (

CGContextRef c,

CGRect rect

);

一次性画出多个矩形

void CGContextAddRects (

CGContextRef c,

const CGRect rects[],

size_t count

);

需要注意的是,画矩形有一些特别,current point没有发生变化

Creating a Path

调用函数 CGContextBeginPath 开始创建路径,线调用函数CGContextMoveToPoint设置起点

然后开始画自己想画的路径,注意一下几点:

1.Lines, arcs, and curves,是从current point开始的

2.假如想封闭一条路径,那么调用函数 CGContextClosePath 把当前点和起点连接起来

3.当在画 arcs的时候,Quartz会画一条线从current point 到 starting point

4.画矩形的时候不会有第三条那这样的的一条直线

5.创建完路径后,必须调用 painting 函数 fill or stroke the path,不然不会画上面东东在相应的设备上】

6.开始创建一个新的路径的时候,使用函数 CGContextBeginPath。

重复利用路径的相关函数和数据类型

CGPathCreateMutable 类似于 CGContextBeginPath

CGPathMoveToPoint 类似于 CGContextMoveToPoint

CGPathAddLineToPoint 类似于 CGContextAddLineToPoint

CGPathAddCurveToPoint 类似于 CGContextAddCurveToPoint

CGPathAddEllipseInRect 类似于 CGContextAddEllipseInRect

CGPathAddArc 类似于 CGContextAddArc

CGPathAddRect 类似于 CGContextAddRect

CGPathCloseSubpath 类似于 CGContextClosePath

CGPathRef

CGMutablePathRef

用CGContextAddPath函数把一个路径添加到graphics context中

void CGContextAddPath (

CGContextRef context,

CGPathRef path

);

Painting a Path
Stroking :画出路径
Filling :填充路径的封闭区域
影响Stroking的参数
Line width
void CGContextSetLineWidth (
CGContextRef c,
CGFloat width
);
Line join:线转弯的时候的样式,比如圆滑的方式
void CGContextSetLineJoin (
CGContextRef c,
CGLineJoin join
);

Line cap:线的两端的样式,比如两端变的圆滑
void CGContextSetLineCap (
CGContextRef c,
CGLineCap cap
);

Miter limit:当Line join的模式是Miter join的时候,这个参数会有影响
void CGContextSetMiterLimit (
CGContextRef c,
CGFloat limit
);
Line dash pattern:虚线相关
void CGContextSetLineDash (
CGContextRef c,
CGFloat phase,
const CGFloat lengths[],
size_t count
);

Stroke color space
void CGContextSetStrokeColorSpace (
CGContextRef c,
CGColorSpaceRef colorspace
);
Stroke color
void CGContextSetStrokeColor (
CGContextRef c,
const CGFloat components[]
);
void CGContextSetStrokeColorWithColor (
CGContextRef c,
CGColorRef color
);
Stroke pattern(和透明度相关)
void CGContextSetStrokePattern (
CGContextRef c,
CGPatternRef pattern,
const CGFloat components[]
);

Stroking的相关函数
Strokes当前path.
void CGContextStrokePath (
CGContextRef c
);
Strokes 指定的 矩形.
void CGContextStrokeRect (
CGContextRef c,
CGRect rect
);

Strokes 指定的 矩形, 使用指定的宽度.
void CGContextStrokeRectWithWidth (
CGContextRef c,
CGRect rect,
CGFloat width
);
Strokes 指定的椭圆.
void CGContextStrokeEllipseInRect (
CGContextRef context,
CGRect rect
);
Strokes 一些直线.
void CGContextStrokeLineSegments (
CGContextRef c,
const CGPoint points[],
size_t count
);
决定是Stroking 还是Filling
void CGContextDrawPath (
CGContextRef c,
CGPathDrawingMode mode
);
Filling a Path
填充一个路径的时候,路径里面的子路径都是独立填充的。
假如是重叠的路径,决定一个点是否被填充,有两种规则
1,nonzero winding number rule:非零绕数规则,假如一个点被从左到右跨过,计数器+1,从右到左跨过,计数器-1,最后,如果结果是0,那么不填充,如果是非零,那么填充。
2,even-odd rule: 奇偶规则,假如一个点被跨过,那么+1,最后是奇数,那么要被填充,偶数则不填充,和方向没有关系。

FunctionDescription
CGContextEOFillPath使用奇偶规则填充当前路径
CGContextFillPath使用非零绕数规则填充当前路径
CGContextFillRect填充指定的矩形
CGContextFillRects填充指定的一些矩形
CGContextFillEllipseInRect填充指定矩形中的椭圆
CGContextDrawPath两个参数决定填充规则,kCGPathFill表示用非零绕数规则,kCGPathEOFill表示用奇偶规则,kCGPathFillStroke表示填充,kCGPathEOFillStroke表示描线,不是填充
Setting Blend Modes
设置当一个颜色覆盖上另外一个颜色,两个颜色怎么混合
默认方式是
result = (alpha * foreground) + (1 - alpha) * background

CGContextSetBlendMode :设置blend mode.
CGContextSaveGState :保存blend mode.
CGContextRestoreGState:在没有保存之前,用这个函数还原blend mode.

下面两张图,第一张是背景图,第二张是前景图,都是不透明的图片

Note: 这个规则也可以应用于图片,用函数:CGContextSetBlendMode 来设置
Normal Blend Mode
这个模式,就是默认的模式,前景图覆盖了背景图.

Multiply Blend Mode
调用函数CGContextSetBlendMode 的时候,使用参数 kCGBlendModeMultiply.
混合了两种颜色,最终的颜色都会比原先的两种颜色暗。

Screen Blend Mode
使用参数:kCGBlendModeScreen
把前景和背景图的颜色先反过来,然后混合,结果混合的地方比先前的颜色都要亮,前景图没有混合到得地方变成白色?

Overlay Blend Mode
使用参数kCGBlendModeOverlay
明亮取决于背景图

Darken Blend Mode
kCGBlendModeDarken

Lighten Blend Mode
kCGBlendModeLighten

Color Dodge Blend Mode
kCGBlendModeColorDodge

Color Burn Blend Mode
kCGBlendModeColorBurn

Soft Light Blend Mode
kCGBlendModeSoftLight

Hard Light Blend Mode
kCGBlendModeHardLight

Difference Blend Mode
kCGBlendModeDifference

Exclusion Blend Mode
kCGBlendModeExclusion

Hue Blend Mode
kCGBlendModeHue

Saturation Blend Mode
kCGBlendModeSaturation

Color Blend Mode
kCGBlendModeColor

Luminosity Blend Mode
kCGBlendModeLuminosity

Clipping to a Path
这个用在,假如我们只想把图片的部分打印到屏幕的时候

CGContextBeginPath (context);
CGContextAddArc (context, w/2, h/2, ((w>h) ? h : w)/2, 0, 2*PI, 0);
CGContextClosePath (context);
CGContextClip (context);
Function

Description

CGContextClip


使用非零绕数规则剪辑当前图形上下文

CGContextEOClip


使用奇偶规则剪辑当前上下文

CGContextClipToRect


设置一个矩形区域和当前的剪辑区域的交集

CGContextClipToRects


设置一些矩形区域和当前剪辑区域的交集

CGContextClipToMask


Maps a mask into the specified rectangle and intersects it with the current clipping area of the graphics context. Any subsequent path drawing you perform to the graphics context is clipped. (See “Masking an Image by Clipping the Context.

参 考http://developer.apple.com/iphone/library/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_paths/dq_paths.html#//apple_ref/doc/uid/TP30001066-CH211-TPXREF101
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: