iOS–绘图介绍

绘制图像的三种方式

一. 子类化UIView,在drawRect:方法画图

执行方法时,系统会自行创建画布(CGContext),并且讲画布推到堆栈的栈顶位置

执行完毕后,系统会执行pop出这个画布。

- (void)drawRect:(CGRect)rect{
CGContextRef con = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(con, CGRectMake(0,0,100,100));
CGContextSetFillColorWithColor(con, [UIColor blueColor].CGColor);
CGContextFillPath(con);
}

二. 直接UIGraphicsBeginImageContextWithOptions绘图

UIGraphicsBeginImageContextWithOptions (

CGSize size,

BOOL opaque,

CGFloat scale

);

参数:size–画布大小

参数:opaque–不透明

参数:scale–放大比例,设置为0,表示和屏幕自适应。(此参数和image中的scale是一样的)

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(100,100), NO, 0);
UIBezierPath* p = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0,0,100,100)];
[[UIColor blueColor] setFill];
[p fill];
UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); UIImageView *imgView = [[UIImageView alloc]initWithImage:im];
imgView.frame = CGRectMake(100, 100, im.size.width, im.size.height);
[self.view addSubview:imgView];

三. layer.delegate代理绘制图片

UIView子类的drawLayer:inContext:方法中实现绘图任务。drawLayer:inContext:方法是一个绘制图层内容的代理方法。为了能够调用drawLayer:inContext:方法,我们需要设定图层的代理对象。

注意点:

1. drawLayer:inContext:代理方法需要写在UIView子类中,如果写在UIViewController中会出现内存过度释放问题。

2. 不应该将UIView对象设置为显示层的委托对象,这是因为UIView对象已经是隐式层的代理对象,再将它设置为另一个层的委托对象就会出问题。

下面是实现代码的声明和实现文件

@interface LayerDelegate : NSObject

@end

@interface LayerDelegateView : UIView

@end
@implementation LayerDelegate

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx{
//1.绘制图形
//画一个圆
CGContextAddEllipseInRect(ctx, CGRectMake(50, 50, 100, 100));
//设置属性(颜色)
CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);
//2.渲染
CGContextFillPath(ctx);
} @end @interface LayerDelegateView () @property (strong, nonatomic) LayerDelegate *delegate; @end @implementation LayerDelegateView - (void)drawRect:(CGRect)rect {
// Drawing code
} - (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
CALayer *myLayer = [CALayer layer];
self.delegate = [[LayerDelegate alloc]init];
myLayer.delegate = self.delegate;
myLayer.backgroundColor = [UIColor brownColor].CGColor;
myLayer.bounds = CGRectMake(0, 0, 200, 150);
myLayer.anchorPoint = CGPointZero;
myLayer.position = CGPointMake(100, 100);
myLayer.cornerRadius = 20;
myLayer.shadowColor = [UIColor blackColor].CGColor;
myLayer.shadowOffset = CGSizeMake(10, 20);
myLayer.shadowOpacity = 0.6; [myLayer setNeedsDisplay]; // 调用此方法,drawLayer: inContext:方法才会被调用。
[self.layer addSublayer:myLayer];
}
return self;
} @end

绘图CGContext堆栈介绍

CGContext被存放在系统的一个绘图上下文堆栈区中,我们编辑CGContext的时候,可以将CGContext push到栈顶,然后在编辑完成时,将GContext pop出栈顶。下面的代码简单介绍了其使用。

    UIImage *img1;
UIImage *img2; //在绘图上下文1中绘图
UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 100), NO, 0.0);
[@"第一个" drawInRect:CGRectMake(10, 20, 80, 20) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}];
img1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsPushContext(UIGraphicsGetCurrentContext());
UIGraphicsEndImageContext();//!!!: 这个等等删掉试试看 //跳转到绘图上下文2中绘图
UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 100), NO, 0.0);
[@"第二个" drawInRect:CGRectMake(10, 20, 80, 20) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}];
img2 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); //pop返回到绘图上下文1中绘图
UIGraphicsPopContext();
[@"再第一个" drawInRect:CGRectMake(110, 20, 80, 20) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}];
img1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsPushContext(UIGraphicsGetCurrentContext());
UIGraphicsEndImageContext(); //pop返回到绘图上下文1中绘图
UIGraphicsPopContext();
[@"再再第一个" drawInRect:CGRectMake(10, 40, 80, 20) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}];
img1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); //绘图上下文1和绘画上下文2中的图片显示出来
UIImageView *imgView1 = [[UIImageView alloc]initWithImage:img1];
imgView1.frame = CGRectMake(0, 100, 200, 100);
imgView1.backgroundColor = [UIColor yellowColor];
[self.view addSubview:imgView1];
UIImageView *imgView2 = [[UIImageView alloc]initWithImage:img2];
imgView2.frame = CGRectMake(0, 300, 200, 100);
imgView2.backgroundColor = [UIColor redColor];
[self.view addSubview:imgView2];

参考文章

iOS绘图教程

iOS绘图实际例子讲解

ios--绘图介绍的更多相关文章

  1. iOS绘图教程 (转,拷贝以记录)

    本文是<Programming iOS5>中Drawing一章的翻译,考虑到主题完整性,在翻译过程中我加入了一些书中没有涉及到的内容.希望本文能够对你有所帮助. 转自:http://www ...

  2. iOS绘图教程

    本文是<Programming iOS5>中Drawing一章的翻译,考虑到主题完整性,翻译版本中加入了一些书中未涉及到的内容.希望本文能够对你有所帮助.(本文由海水的味道翻译整理,转载请 ...

  3. iOS框架介绍

    iOS框架介绍      Cocoa Touch   GameKit  实现对游戏中心的支持,让用户能够在线共享他们的游戏相关的信息  iOS设备之间蓝牙数据传输   从iOS7开始过期   局域网游 ...

  4. iOS绘图框架CoreGraphics分析

    由于CoreGraphics框架有太多的API,对于初次接触或者对该框架不是十分了解的人,在绘图时,对API的选择会感到有些迷茫,甚至会觉得iOS的图形绘制有些繁琐.因此,本文主要介绍一下iOS的绘图 ...

  5. iOS绘图系统UIKit与Core Graphics

    概述 iOS主要的绘图系统有UIKit,Core Graphics,Core Animation,Core Image,Open GL等,本片博文主要介绍UIKit与Core Graphics的绘图系 ...

  6. 论文第4章:iOS绘图平台的实现

    面向移动设备的矢量绘图平台设计与实现 Design and Implementation of Mobile Device-oriented Vector Drawing Platform 引用本论文 ...

  7. IOS绘图

    #import "ViewController.h" #import "DrawView.h" @interface ViewController () @pr ...

  8. iOS CoreData 介绍和使用(以及一些注意事项)

    iOS CoreData介绍和使用(以及一些注意事项) 最近花了一点时间整理了一下CoreData,对于经常使用SQLite的我来说,用这个真的有点用不惯,个人觉得实在是没发现什么亮点,不喜勿喷啊.不 ...

  9. iOS CoreData介绍和使用(以及一些注意事项)

    iOS CoreData介绍和使用(以及一些注意事项) 最近花了一点时间整理了一下CoreData,对于经常使用SQLite的我来说,用这个真的有点用不惯,个人觉得实在是没发现什么亮点,不喜勿喷啊.不 ...

  10. python 绘图介绍

    1. python 绘图介绍 2. 函数 import numpy as np import matplotlib.pyplot as plt t = np.arange(0.0, 3.0, 0.01 ...

随机推荐

  1. "The connection for the USB device '###' was unsuccessful. The device is currently in use"

    一.问题描述 1.情景描述 笔者的物理主机系统是“windows7 64位”,想使用“摄像头录像大师”.这个软件在录制视频的过程中,需要调用windows自带的"windows media ...

  2. Label设置行间距--b

    内容摘要 UILabel显示多行文本 UILabel设置行间距 解决单行文本 & 多行文本显示的问题 场景描述 众所周知,UILabel显示多行的话,默认行间距为0,但实际开发中,如果显示多行 ...

  3. 搭建Tornado+Nginx

    Tornado一个高效的异步非阻塞式的实时Web服务器,是Facebook旗下的 FriendFeed 网站开源Web服务器版本.但是它内置的HTTP服务器功能有限,不能在生产环境下使用. 在 Fri ...

  4. xcode 环境,多工程联编设置【转】

    http://blog.csdn.net/vienna_zj/article/details/8467522 一.xcode4中的环境变量 $(BUILT_PRODUCTS_DIR) build成功后 ...

  5. Sed&awk笔记之awk篇

    http://blog.csdn.net/a81895898/article/details/8482333 Awk是什么 Awk.sed与grep,俗称Linux下的三剑客,它们之间有很多相似点,但 ...

  6. Class.forName()的理解

    转自:http://blog.csdn.net/yanwushu/article/details/7574713 使用jdbc方式连接数据库时会使用一句代码Class.forName(String c ...

  7. 分布式文件系统MFS(moosefs)实现存储共享(第二版)

    分布式文件系统MFS(moosefs)实现存储共享(第二版) 作者:田逸(sery@163.com) 由于用户数量的不断攀升,我对访问量大的应用实现了可扩展.高可靠的集群部署(即lvs+keepali ...

  8. ☀【HTML5】Modernizr

    Modernizr 使用Modernizr探测HTML5/CSS3新特性

  9. java基础(十二)常用类总结(二)

    这里有我之前上课总结的一些知识点以及代码大部分是老师讲的笔记 个人认为是非常好的,,也是比较经典的内容,真诚的希望这些对于那些想学习的人有所帮助! 由于代码是分模块的上传非常的不便.也比较多,讲的也是 ...

  10. UVA 11427 Expect the Expected(DP+概率)

    链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35396 [思路] DP+概率 见白书. [代码] #include&l ...