本文转载至 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. 用js怎么控制submit提交表单

    需求: 1. 要在点击submit按钮的时候,弹出一个询问框,"你确定要修改?".如果按了"确定"那么就提交表单,否则就保留在原页面,既不提交不跳转. 2. 要 ...

  2. selfshadow

    realtime rendering v3 page 351 Moire pattern sruface acne artifacts ----------------------- 用 setsta ...

  3. jstl标签设置通用web项目根路径

    在做项目时(如SSH或SpringMVC),通常需要在很多页面(jsp中的form提交)或者js代码(一般Ajax提交)中用到当前web应用的根路径(拼成访问资源如action/controller. ...

  4. 跟我一起透彻理解template模板模式

    #include <iostream> using namespace std; //template模式. class Base { public: void DealWhat() { ...

  5. Intellij idea远程debug连接tomcat,实现单步调试

    转载:http://blog.csdn.net/boling_cavalry/article/details/73384036 web项目部署到tomcat上之后,有时需要打断点单步调试,如果用的是I ...

  6. json-server模拟接口获取mock数据

    转载:http://blog.csdn.net/stevennest/article/details/76167343 安装json-server 运行以下命令 cnpm install json-s ...

  7. 性能测试篇 :Jmeter监控服务器性能

    转载:http://www.cnblogs.com/chengtch/p/6079262.html jmeter也可以像loadrunner一样监控服务器CPU.内存等性能参数,不过需要安装一些插件 ...

  8. Android模块编译过程中的错误no rules to make target

    今天花了不少时间在纠正一个编译错误: make: *** No rule to make target `out/target/common/obj/JAVA_LIBRARIES/sqlite-jdb ...

  9. [转]php-fpm配置具体解释

    [转]php-fpm配置具体解释 php-fpm具体解释 原文链接:http://php-fpm.anight.org/wiki:http://www.php-fpm.com/翻译:http://sy ...

  10. ROS当中添加第三方库

      下文以serial 库为依据讲解第三方库在ROS下面的配置. 参考文献:http://blog.csdn.net/u011853479/article/details/51263590 ros中使 ...