画阴影:

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,最后是奇数,那么要被填充,偶数则不填充,和方向没有关系。

 
 Function Description 
 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.

 

iOS-图形绘制(全)的更多相关文章

  1. iOS:quartz2D绘图(给图形绘制阴影)

    quartz2D既可以绘制原始图形,也可以给原始图形绘制阴影. 绘制阴影时,需要的一些参数:上下文.阴影偏移量.阴影模糊系数 注意:在drawRect:方法中同时调用绘制同一个图形时,在对绘制的图形做 ...

  2. iOS:iOS开发非常全的三方库、插件等等

    iOS开发非常全的三方库.插件等等 github排名:https://github.com/trending, github搜索:https://github.com/search. 此文章转自git ...

  3. iOS的非常全的三方库,插件,大牛博客

    转自: http://www.cnblogs.com/zyjzyj/p/6015625.html github排名:https://github.com/trending, github搜索:http ...

  4. iOS开发 非常全的三方库、插件、大牛博客等等

    UI 下拉刷新 EGOTableViewPullRefresh- 最早的下拉刷新控件. SVPullToRefresh- 下拉刷新控件. MJRefresh- 仅需一行代码就可以为UITableVie ...

  5. [ios]iOS 图形编程总结

    转自:http://www.cocoachina.com/ios/20141104/10124.html iOS实现图形编程可以使用三种API(UIKIT.Core Graphics.OpenGL E ...

  6. iOS 图形编程总结

    iOS实现图形编程可以使用三种API(UIKIT.Core Graphics.OpenGL ES及GLKit). 这些api包含的绘制操作都在一个图形环境中进行绘制.一个图形环境包含绘制参数和所有的绘 ...

  7. 一个由正则表达式引发的血案 vs2017使用rdlc实现批量打印 vs2017使用rdlc [asp.net core 源码分析] 01 - Session SignalR sql for xml path用法 MemCahe C# 操作Excel图形——绘制、读取、隐藏、删除图形 IOC,DIP,DI,IoC容器

    1. 血案由来 近期我在为Lazada卖家中心做一个自助注册的项目,其中的shop name校验规则较为复杂,要求:1. 英文字母大小写2. 数字3. 越南文4. 一些特殊字符,如“&”,“- ...

  8. iOS之绘制像素到屏幕

    译注:这篇文章虽然比较长,但是里面的内容还是很有价值的. 像素是如何绘制到屏幕上面的?把数据输出到屏幕的方法有很多,通过调用很多不同的framework和不同的函数.这里我们讲一下这个过程背后的东西. ...

  9. 【Windows编程】系列第五篇:GDI图形绘制

    上两篇我们学习了文本字符输出以及Unicode编写程序,知道如何用常见Win32输出文本字符串,这一篇我们来学习Windows编程中另一个非常重要的部分GDI图形绘图.Windows的GDI函数包含数 ...

  10. 13个JavaScript图表(JS图表)图形绘制插件【转】

    现在网络上又有越来越多的免费的(JS 图表)JavaScript图表图形绘制插件.我之前给一家网站做过复杂的图形,我们用的是 highchart.在那段时间,没有很多可供选择的插件.但现在不同了,很容 ...

随机推荐

  1. JAVA系列:浅谈Java中的equals和==

    在初学Java时,可能会经常碰到下面的代码: 1 String str1 = new String("hello"); 2 String str2 = new String(&qu ...

  2. dede织梦调用顶级二级栏目及下三级栏目方法(数据库实现)

    上次有说道能调用织梦的二级栏目今天来说道说道调用三级,乃至无限极 ①:通过dede调用二级栏目大家都会调用,但要调用三级栏目,就有点麻烦了,如下样式的三级栏目dede如何调用呢?如下: ------- ...

  3. [51Nod 1244] - 莫比乌斯函数之和 & [51Nod 1239] - 欧拉函数之和 (杜教筛板题)

    [51Nod 1244] - 莫比乌斯函数之和 求∑i=1Nμ(i)\sum_{i=1}^Nμ(i)∑i=1N​μ(i) 开推 ∑d∣nμ(d)=[n==1]\sum_{d|n}\mu(d)=[n== ...

  4. POJ-2478-Farey Sequence(欧拉函数)

    链接: https://vjudge.net/problem/POJ-2478 题意: The Farey Sequence Fn for any integer n with n >= 2 i ...

  5. 使用集合方式注入IoC

    使用集合方式注入Ioc 1.创建类 //集合 private String[] arrays; //list集合 private List<Integer> lists; //map集合 ...

  6. Devtool-Console

    1. console面板展示 1.全屏展示 打开开发者工具(option+cmd+i),点击console的tab 2. 在其他面板展示的同时展示console面板 1)esc快捷命令 2)或者选择打 ...

  7. html5文件夹上传源码

    前段时间做视频上传业务,通过网页上传视频到服务器. 视频大小 小则几十M,大则 1G+,以一般的HTTP请求发送数据的方式的话,会遇到的问题:1,文件过大,超出服务端的请求大小限制:2,请求时间过长, ...

  8. 数据结构实验之查找四:二分查找(SDUT 3376)

    #include <stdio.h> #include <string.h> #include <stdlib.h> int a[1000005]; int fin ...

  9. jQuery的ajax请求express服务器返回数据

    html页面 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  10. NOIP2018普及组初赛解题报告

    本蒟蒻参加了今年的NOIP2018普及组的初赛 感觉要凉 总而言之,今年的题要说完全没有难度倒也不至于,还有不少拼RP的题,比如第一次问题求解考逻辑推理,第一次完善程序考双链表等 下面我就和大家一起看 ...