本文转载至 http://blog.csdn.net/weisubao/article/details/41282457

  1. - (void)drawRect:(CGRect)rect {
  2. //获得当前上下文
  3. CGContextRef ctx=UIGraphicsGetCurrentContext();
  4. //把当前上下文状态保存在栈中
  5. CGContextSaveGState(ctx);
  6. //缩放、移动处理(需要放在画图之前进行设置)
  7. CGContextScaleCTM(ctx, 0.5, 0.5);
  8. CGContextTranslateCTM(ctx, 100, 100);
  9. CGContextRotateCTM(ctx, M_PI_4);
  10. //描点
  11. CGContextMoveToPoint(ctx, 10, 10);
  12. CGContextAddLineToPoint(ctx, 100, 100);
  13. CGContextAddLineToPoint(ctx, 150, 50);
  14. //以下两种方式均可闭环
  15. //CGContextAddLineToPoint(ctx, 10, 10);
  16. CGContextClosePath(ctx);
  17. //渲染绘图,实心和空心
  18. CGContextStrokePath(ctx);
  19. //CGContextFillPath(ctx);
  20. //把当前上下文状态保存在栈中
  21. CGContextSaveGState(ctx);
  22. //画正方形
  23. CGContextAddRect(ctx, CGRectMake(100, 100, 50, 50));
  24. //设置线宽(一定要在CGContextStrokePath之前)
  25. //因为之前有过一次渲染绘图,所以这个属性设置不影响上面的那个三角形,以下颜色设置同理
  26. //所以,如果想分别设置两个或多个图形的属性,就分别渲染绘图一次
  27. CGContextSetLineWidth(ctx, 10);
  28. //设置颜色(同理,属性设置的代码都要在绘图的代码之前)
  29. CGContextSetRGBStrokeColor(ctx, 1, 0, 0, 1);
  30. CGContextStrokePath(ctx);
  31. //设置样式
  32. CGContextMoveToPoint(ctx, 20, 160);
  33. CGContextAddLineToPoint(ctx, 200, 280);
  34. CGContextAddLineToPoint(ctx, 250, 200);
  35. CGContextSetLineWidth(ctx, 20);
  36. //设置头尾样式
  37. CGContextSetLineCap(ctx, kCGLineCapRound);
  38. //设置转角样式
  39. CGContextSetLineJoin(ctx, kCGLineJoinRound);
  40. CGContextStrokePath(ctx);
  41. //把保存在栈中的上下文状态取出来,恢复。上面那段代码设置的样式不会影响其他
  42. CGContextRestoreGState(ctx);
  43. //画椭圆
  44. CGContextAddEllipseInRect(ctx, CGRectMake(200, 130, 60, 30));
  45. //以下等价
  46. //CGContextStrokePath(ctx);
  47. CGContextDrawPath(ctx, kCGPathStroke);
  48. //画圆形
  49. CGContextAddEllipseInRect(ctx, CGRectMake(140, 170, 50, 50));
  50. CGContextSetLineWidth(ctx, 3);
  51. CGContextStrokePath(ctx);
  52. //画圆弧
  53. CGContextAddArc(ctx, 200, 50, 50, M_PI_4, M_PI, 1);
  54. CGContextStrokePath(ctx);
  55. //画1/4圆,以及颜色的设置新方法
  56. CGContextMoveToPoint(ctx, 10, 230);
  57. CGContextAddLineToPoint(ctx, 10, 280);
  58. CGContextAddLineToPoint(ctx, 60, 280);
  59. CGContextAddArc(ctx, 10, 280, 50, 0, -M_PI_2, 1);
  60. [[UIColor greenColor] setStroke];
  61. CGContextStrokePath(ctx);
  62. //画图片和文字(不需要手动取得上下文)
  63. NSString *str1=@"辛丑年一空作";
  64. [str1 drawAtPoint:CGPointZero withAttributes:nil];
  65. UIImage *img=[UIImage imageNamed:@"001"];
  66. [img drawAtPoint:CGPointMake(10, 10)];
  67. //在一个框框里重叠图片并署名
  68. CGRect rect1=CGRectMake(50, 50, 100, 100);
  69. [img drawAsPatternInRect:rect1];
  70. NSMutableDictionary *attr=[[NSMutableDictionary alloc]init];
  71. attr[NSForegroundColorAttributeName]=[UIColor whiteColor];
  72. attr[NSFontAttributeName]=[UIFont systemFontOfSize:13];
  73. [str1 drawInRect:CGRectMake(50, 140, 100, 100) withAttributes:attr];
  74. //把保存在栈中的上下文状态取出来,恢复。上面那段代码设置的样式不会影响其他
  75. CGContextRestoreGState(ctx);
  76. //裁剪圆形头像
  77. CGContextAddEllipseInRect(ctx, CGRectMake(150, 150, 100 , 100));
  78. //按照圆形剪裁出一个上下文区域,以后的内容就填充在这个圆形上下文中
  79. CGContextClip(ctx);
  80. UIImage *img1=[UIImage imageNamed:@"me"];
  81. [img1 drawAtPoint:CGPointMake(150, 150)];
  82. }

【iOS开发-80】Quartz2D绘图简介:直线/圆形/椭圆/方形以及上下文栈管理CGContextSaveGState/CGContextRestoreGState的更多相关文章

  1. 【iOS开发-80】Quartz2D画图简单介绍:直线/圆形/椭圆/方形以及上下文栈管理CGContextSaveGState/CGContextRestoreGState

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2Vpc3ViYW8=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...

  2. iOS开发UI篇—CALayer简介

    iOS开发UI篇—CALayer简介   一.简单介绍 在iOS中,你能看得见摸得着的东西基本上都是UIView,比如一个按钮.一个文本标签.一个文本输入框.一个图标等等,这些都是UIView. 其实 ...

  3. iOS开发多线程篇—多线程简介

    iOS开发多线程篇-多线程简介 一.进程和线程 1.什么是进程 进程是指在系统中正在执行的一个应用程序 每一个进程之间是独立的.每一个进程均执行在其专用且受保护的内存空间内 比方同一时候打开QQ.Xc ...

  4. iOS开发进阶--1.多线程简介

    学习是由已知的知识模型推理未知的知识模型的的过程. 本文适合学习完objective-c基础,想进一步提高做iOS开发的同学阅读. 在说线程的时候,我们先看看进程. 1.进程 每一个运行在系统中的应用 ...

  5. iOS开发多线程篇—GCD简介

    iOS开发多线程篇—GCD介绍 一.简单介绍 1.什么是GCD? 全称是Grand Central Dispatch,可译为“牛逼的中枢调度器” 纯C语言,提供了非常多强大的函数 2.GCD的优势 G ...

  6. 在iOS开发的Quartz2D使用中实现图片剪切和截屏功能

    原文  http://www.jb51.net/article/75671.htm 图片剪切一.使用Quartz2D完成图片剪切1.把图片显示在自定义的view中先把图片绘制到view上.按照原始大小 ...

  7. iOS开发Drag and Drop简介

    1.Drag and Drop简介 Drag and Drop是iOS11的新特性,可以将文本.图片进行拖拽到不同app中,实现数据的传递.只不过只能在iPad上使用,iPhone上只能app内部拖拽 ...

  8. iOS开发--CoreGraphics简单绘图

    一.导入coreGraphics.framework 二.绘制图形 1.绘制矩形 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // 绘制矩形 - (v ...

  9. iOS开发UITouch触摸API简介

    1.UITouch简介 当用户触摸屏幕时,会创建一个UITouch对象: UITouch的作用保存着触摸相关的信息,比如触摸的位置.时间.阶段等: 当从开始到结束,系统会更新UITouch对象,结束时 ...

随机推荐

  1. 重新认识被人遗忘的HTTP头注入

    前言 注入类漏洞经久不衰,多年保持在owasp Top 10的首位.今天就聊聊那些被人遗忘的http头注入.用简单的实际代码进行演示,让每个人更深刻的去认识该漏洞. HOST注入 在以往http1.0 ...

  2. crossapp里的位置设置

    crossapp里有Frame.Center,这两种都是可以用来确定一个view的位置和大小. 不同点:Frame定位是以View的左上角为参照点,Center是以View的中心点为参照点 注意cro ...

  3. elasticsearch升级步骤

    ES从1.2.1升级到1.4.0 升级步骤,基本上是按照官网的叙述来完成的,链接是:http://www.elasticsearch.org/guide/en/elasticsearch/refere ...

  4. 转:Hash, MAC,HMAC说明

    from: http://www.cnblogs.com/songhan/archive/2012/07/29/2613898.html Hash, MAC,HMAC Hash-MD5, SHA-1, ...

  5. [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 ...

  6. 安全狗两个中危提权+NET提权

    1.循环加组复现 for /l %%i in (1,1,1000) do @net user admin admin /add&@ net localgroup administrators  ...

  7. functools.wraps

    我们在使用 Decorator 的过程中,难免会损失一些原本的功能信息.直接拿 stackoverflow 里面的栗子     1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...

  8. 最小化JavaScript代码

    1.去除不必要的格式符.空白符.凝视符. 这个操作.事实上能够理解为是一种格式化.尽管它操作的结果事实上是去除掉原始文件的那些格式. 2.模糊(Obfuscation)处理JAVASCRIP脚本源码. ...

  9. springboot @Configuration

    有了@Configuration,原来的springBean的配置文件可以去掉了, 原来在application.xml中配置的bean可以配置在@Configuration注解的来类中,使用@Bea ...

  10. react-native 初始化 各种报错 及 解决方案

    1.Unable to load script from assets 'index.android.bundle'. curl -k "http://localhost:8081/inde ...