【iOS开发-80】Quartz2D绘图简介:直线/圆形/椭圆/方形以及上下文栈管理CGContextSaveGState/CGContextRestoreGState
- - (void)drawRect:(CGRect)rect {
- //获得当前上下文
- CGContextRef ctx=UIGraphicsGetCurrentContext();
- //把当前上下文状态保存在栈中
- CGContextSaveGState(ctx);
- //缩放、移动处理(需要放在画图之前进行设置)
- CGContextScaleCTM(ctx, 0.5, 0.5);
- CGContextTranslateCTM(ctx, 100, 100);
- CGContextRotateCTM(ctx, M_PI_4);
- //描点
- CGContextMoveToPoint(ctx, 10, 10);
- CGContextAddLineToPoint(ctx, 100, 100);
- CGContextAddLineToPoint(ctx, 150, 50);
- //以下两种方式均可闭环
- //CGContextAddLineToPoint(ctx, 10, 10);
- CGContextClosePath(ctx);
- //渲染绘图,实心和空心
- CGContextStrokePath(ctx);
- //CGContextFillPath(ctx);
- //把当前上下文状态保存在栈中
- CGContextSaveGState(ctx);
- //画正方形
- CGContextAddRect(ctx, CGRectMake(100, 100, 50, 50));
- //设置线宽(一定要在CGContextStrokePath之前)
- //因为之前有过一次渲染绘图,所以这个属性设置不影响上面的那个三角形,以下颜色设置同理
- //所以,如果想分别设置两个或多个图形的属性,就分别渲染绘图一次
- CGContextSetLineWidth(ctx, 10);
- //设置颜色(同理,属性设置的代码都要在绘图的代码之前)
- CGContextSetRGBStrokeColor(ctx, 1, 0, 0, 1);
- CGContextStrokePath(ctx);
- //设置样式
- CGContextMoveToPoint(ctx, 20, 160);
- CGContextAddLineToPoint(ctx, 200, 280);
- CGContextAddLineToPoint(ctx, 250, 200);
- CGContextSetLineWidth(ctx, 20);
- //设置头尾样式
- CGContextSetLineCap(ctx, kCGLineCapRound);
- //设置转角样式
- CGContextSetLineJoin(ctx, kCGLineJoinRound);
- CGContextStrokePath(ctx);
- //把保存在栈中的上下文状态取出来,恢复。上面那段代码设置的样式不会影响其他
- CGContextRestoreGState(ctx);
- //画椭圆
- CGContextAddEllipseInRect(ctx, CGRectMake(200, 130, 60, 30));
- //以下等价
- //CGContextStrokePath(ctx);
- CGContextDrawPath(ctx, kCGPathStroke);
- //画圆形
- CGContextAddEllipseInRect(ctx, CGRectMake(140, 170, 50, 50));
- CGContextSetLineWidth(ctx, 3);
- CGContextStrokePath(ctx);
- //画圆弧
- CGContextAddArc(ctx, 200, 50, 50, M_PI_4, M_PI, 1);
- CGContextStrokePath(ctx);
- //画1/4圆,以及颜色的设置新方法
- CGContextMoveToPoint(ctx, 10, 230);
- CGContextAddLineToPoint(ctx, 10, 280);
- CGContextAddLineToPoint(ctx, 60, 280);
- CGContextAddArc(ctx, 10, 280, 50, 0, -M_PI_2, 1);
- [[UIColor greenColor] setStroke];
- CGContextStrokePath(ctx);
- //画图片和文字(不需要手动取得上下文)
- NSString *str1=@"辛丑年一空作";
- [str1 drawAtPoint:CGPointZero withAttributes:nil];
- UIImage *img=[UIImage imageNamed:@"001"];
- [img drawAtPoint:CGPointMake(10, 10)];
- //在一个框框里重叠图片并署名
- CGRect rect1=CGRectMake(50, 50, 100, 100);
- [img drawAsPatternInRect:rect1];
- NSMutableDictionary *attr=[[NSMutableDictionary alloc]init];
- attr[NSForegroundColorAttributeName]=[UIColor whiteColor];
- attr[NSFontAttributeName]=[UIFont systemFontOfSize:13];
- [str1 drawInRect:CGRectMake(50, 140, 100, 100) withAttributes:attr];
- //把保存在栈中的上下文状态取出来,恢复。上面那段代码设置的样式不会影响其他
- CGContextRestoreGState(ctx);
- //裁剪圆形头像
- CGContextAddEllipseInRect(ctx, CGRectMake(150, 150, 100 , 100));
- //按照圆形剪裁出一个上下文区域,以后的内容就填充在这个圆形上下文中
- CGContextClip(ctx);
- UIImage *img1=[UIImage imageNamed:@"me"];
- [img1 drawAtPoint:CGPointMake(150, 150)];
- }
【iOS开发-80】Quartz2D绘图简介:直线/圆形/椭圆/方形以及上下文栈管理CGContextSaveGState/CGContextRestoreGState的更多相关文章
- 【iOS开发-80】Quartz2D画图简单介绍:直线/圆形/椭圆/方形以及上下文栈管理CGContextSaveGState/CGContextRestoreGState
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2Vpc3ViYW8=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...
- iOS开发UI篇—CALayer简介
iOS开发UI篇—CALayer简介 一.简单介绍 在iOS中,你能看得见摸得着的东西基本上都是UIView,比如一个按钮.一个文本标签.一个文本输入框.一个图标等等,这些都是UIView. 其实 ...
- iOS开发多线程篇—多线程简介
iOS开发多线程篇-多线程简介 一.进程和线程 1.什么是进程 进程是指在系统中正在执行的一个应用程序 每一个进程之间是独立的.每一个进程均执行在其专用且受保护的内存空间内 比方同一时候打开QQ.Xc ...
- iOS开发进阶--1.多线程简介
学习是由已知的知识模型推理未知的知识模型的的过程. 本文适合学习完objective-c基础,想进一步提高做iOS开发的同学阅读. 在说线程的时候,我们先看看进程. 1.进程 每一个运行在系统中的应用 ...
- iOS开发多线程篇—GCD简介
iOS开发多线程篇—GCD介绍 一.简单介绍 1.什么是GCD? 全称是Grand Central Dispatch,可译为“牛逼的中枢调度器” 纯C语言,提供了非常多强大的函数 2.GCD的优势 G ...
- 在iOS开发的Quartz2D使用中实现图片剪切和截屏功能
原文 http://www.jb51.net/article/75671.htm 图片剪切一.使用Quartz2D完成图片剪切1.把图片显示在自定义的view中先把图片绘制到view上.按照原始大小 ...
- iOS开发Drag and Drop简介
1.Drag and Drop简介 Drag and Drop是iOS11的新特性,可以将文本.图片进行拖拽到不同app中,实现数据的传递.只不过只能在iPad上使用,iPhone上只能app内部拖拽 ...
- iOS开发--CoreGraphics简单绘图
一.导入coreGraphics.framework 二.绘制图形 1.绘制矩形 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // 绘制矩形 - (v ...
- iOS开发UITouch触摸API简介
1.UITouch简介 当用户触摸屏幕时,会创建一个UITouch对象: UITouch的作用保存着触摸相关的信息,比如触摸的位置.时间.阶段等: 当从开始到结束,系统会更新UITouch对象,结束时 ...
随机推荐
- 重新认识被人遗忘的HTTP头注入
前言 注入类漏洞经久不衰,多年保持在owasp Top 10的首位.今天就聊聊那些被人遗忘的http头注入.用简单的实际代码进行演示,让每个人更深刻的去认识该漏洞. HOST注入 在以往http1.0 ...
- crossapp里的位置设置
crossapp里有Frame.Center,这两种都是可以用来确定一个view的位置和大小. 不同点:Frame定位是以View的左上角为参照点,Center是以View的中心点为参照点 注意cro ...
- elasticsearch升级步骤
ES从1.2.1升级到1.4.0 升级步骤,基本上是按照官网的叙述来完成的,链接是:http://www.elasticsearch.org/guide/en/elasticsearch/refere ...
- 转:Hash, MAC,HMAC说明
from: http://www.cnblogs.com/songhan/archive/2012/07/29/2613898.html Hash, MAC,HMAC Hash-MD5, SHA-1, ...
- [PWA] Add Push Notifications to a PWA with React in Chrome and on Android
On Android and in Chrome (but not on iOS), it's possible to send push notifications with a PWA. We'l ...
- 安全狗两个中危提权+NET提权
1.循环加组复现 for /l %%i in (1,1,1000) do @net user admin admin /add&@ net localgroup administrators ...
- functools.wraps
我们在使用 Decorator 的过程中,难免会损失一些原本的功能信息.直接拿 stackoverflow 里面的栗子 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- 最小化JavaScript代码
1.去除不必要的格式符.空白符.凝视符. 这个操作.事实上能够理解为是一种格式化.尽管它操作的结果事实上是去除掉原始文件的那些格式. 2.模糊(Obfuscation)处理JAVASCRIP脚本源码. ...
- springboot @Configuration
有了@Configuration,原来的springBean的配置文件可以去掉了, 原来在application.xml中配置的bean可以配置在@Configuration注解的来类中,使用@Bea ...
- react-native 初始化 各种报错 及 解决方案
1.Unable to load script from assets 'index.android.bundle'. curl -k "http://localhost:8081/inde ...