iOS-图形绘制(全)
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绘图整理一(转)
会有一条直线,从current point到(x1,y1)
假如第二个控制点(cp2x,cp2y)比(cp1x,cp1y) 更接近current point,那么会形成一个封闭的曲线
执行完函数貌似current point不会变化,没有具体测试过
Miter limit:当Line join的模式是Miter join的时候,这个参数会有影响
Function | Description |
CGContextEOFillPath | 使用奇偶规则填充当前路径 |
CGContextFillPath | 使用非零绕数规则填充当前路径 |
CGContextFillRect | 填充指定的矩形 |
CGContextFillRects | 填充指定的一些矩形 |
CGContextFillEllipseInRect | 填充指定矩形中的椭圆 |
CGContextDrawPath | 两个参数决定填充规则,kCGPathFill表示用非零绕数规则,kCGPathEOFill表示用奇偶规则,kCGPathFillStroke表示填充,kCGPathEOFillStroke表示描线,不是填充 |
Setting Blend Modes
Note: 这个规则也可以应用于图片,用函数:CGContextSetBlendMode来设置 |
混合了两种颜色,最终的颜色都会比原先的两种颜色暗。
把前景和背景图的颜色先反过来,然后混合,结果混合的地方比先前的颜色都要亮,前景图没有混合到得地方变成白色?
明亮取决于背景图
kCGBlendModeDarken
kCGBlendModeLighten
kCGBlendModeColorDodge
kCGBlendModeColorBurn
kCGBlendModeSoftLight
kCGBlendModeHardLight
kCGBlendModeDifference
Hue Blend Mode
kCGBlendModeSaturation
kCGBlendModeLuminosity
这个用在,假如我们只想把图片的部分打印到屏幕的时候
CGContextBeginPath (context); |
CGContextAddArc (context, w/2, h/2, ((w>h) ? h : w)/2, 0, 2*PI, 0); |
CGContextClosePath (context); |
CGContextClip (context); |
Function |
Description |
---|---|
使用非零绕数规则剪辑当前图形上下文 |
|
使用奇偶规则剪辑当前上下文 |
|
设置一个矩形区域和当前的剪辑区域的交集 |
|
设置一些矩形区域和当前剪辑区域的交集 |
|
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-图形绘制(全)的更多相关文章
- iOS:quartz2D绘图(给图形绘制阴影)
quartz2D既可以绘制原始图形,也可以给原始图形绘制阴影. 绘制阴影时,需要的一些参数:上下文.阴影偏移量.阴影模糊系数 注意:在drawRect:方法中同时调用绘制同一个图形时,在对绘制的图形做 ...
- iOS:iOS开发非常全的三方库、插件等等
iOS开发非常全的三方库.插件等等 github排名:https://github.com/trending, github搜索:https://github.com/search. 此文章转自git ...
- iOS的非常全的三方库,插件,大牛博客
转自: http://www.cnblogs.com/zyjzyj/p/6015625.html github排名:https://github.com/trending, github搜索:http ...
- iOS开发 非常全的三方库、插件、大牛博客等等
UI 下拉刷新 EGOTableViewPullRefresh- 最早的下拉刷新控件. SVPullToRefresh- 下拉刷新控件. MJRefresh- 仅需一行代码就可以为UITableVie ...
- [ios]iOS 图形编程总结
转自:http://www.cocoachina.com/ios/20141104/10124.html iOS实现图形编程可以使用三种API(UIKIT.Core Graphics.OpenGL E ...
- iOS 图形编程总结
iOS实现图形编程可以使用三种API(UIKIT.Core Graphics.OpenGL ES及GLKit). 这些api包含的绘制操作都在一个图形环境中进行绘制.一个图形环境包含绘制参数和所有的绘 ...
- 一个由正则表达式引发的血案 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. 一些特殊字符,如“&”,“- ...
- iOS之绘制像素到屏幕
译注:这篇文章虽然比较长,但是里面的内容还是很有价值的. 像素是如何绘制到屏幕上面的?把数据输出到屏幕的方法有很多,通过调用很多不同的framework和不同的函数.这里我们讲一下这个过程背后的东西. ...
- 【Windows编程】系列第五篇:GDI图形绘制
上两篇我们学习了文本字符输出以及Unicode编写程序,知道如何用常见Win32输出文本字符串,这一篇我们来学习Windows编程中另一个非常重要的部分GDI图形绘图.Windows的GDI函数包含数 ...
- 13个JavaScript图表(JS图表)图形绘制插件【转】
现在网络上又有越来越多的免费的(JS 图表)JavaScript图表图形绘制插件.我之前给一家网站做过复杂的图形,我们用的是 highchart.在那段时间,没有很多可供选择的插件.但现在不同了,很容 ...
随机推荐
- python - threading.local
import time import threading try: # 线程和协程都可处理 import greenlet get_ident = greenlet.getcurrent except ...
- Shared Nothing、Shared Everthting、Shared Disk
数据库构架设计中主要有Shared Everthting.Shared Nothing.和Shared Disk:1.Shared Everything:一般是针对单个主机,完全透明共享CPU/MEM ...
- react页面跳转 window.location.href和window.open的几种用法和区别
https://www.cnblogs.com/Qian123/p/5345298.html
- TensorFlow(十五):使用inception-v3实现各种图像识别
上代码: import tensorflow as tf import os import numpy as np import re from PIL import Image import mat ...
- Pycharm使用常见问题
Pycharm下载 下载链接:https://www.jetbrains.com/pycharm/download/ 分为专业版和社区版,社区版也能满足学习需求 Pycharm专业版激活 使用前请将& ...
- 如何查看Linux cpu核数、版本等信息
CPU总核数 = 物理CPU个数 * 每颗物理CPU的核数 总逻辑CPU数 = 物理CPU个数 * 每颗物理CPU的核数 * 超线程数 1.查看CPU信息(型号): [root@iZ2ze1rl2qy ...
- Java核心复习——synchronized
一.概念 利用锁机制实现线程同步,synchronized关键字的底层交由了JVM通过C++来实现 Java中的锁有两大特性: 互斥性 同一时间,只允许一个线程持有某个对象锁. 可见性 锁释放前,线程 ...
- Spring boot 集成Solr
首先安装Solr 集成 ikanalyzer ,可以参考 https://www.cnblogs.com/lick468/p/10867492.html https://www.cnblogs.com ...
- ngx.shared.DICT.get 详解
ngx.shared.DICT.get 原文: ngx.shared.DICT.get syntax: value, flags = ngx.shared.DICT:get(key) context: ...
- HearthBuddy 调试肯瑞托法师寒冰屏障的配合
35疯狂的科学家 63肯瑞托法师 13过期货物专卖商 64对面的英雄术士 比较好的出牌策略是,肯瑞托法师+寒冰屏障 ailoop1 startEnemyTurnSimThread1start prin ...