ios--绘图介绍
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绘图教程 (转,拷贝以记录)
本文是<Programming iOS5>中Drawing一章的翻译,考虑到主题完整性,在翻译过程中我加入了一些书中没有涉及到的内容.希望本文能够对你有所帮助. 转自:http://www ...
- iOS绘图教程
本文是<Programming iOS5>中Drawing一章的翻译,考虑到主题完整性,翻译版本中加入了一些书中未涉及到的内容.希望本文能够对你有所帮助.(本文由海水的味道翻译整理,转载请 ...
- iOS框架介绍
iOS框架介绍 Cocoa Touch GameKit 实现对游戏中心的支持,让用户能够在线共享他们的游戏相关的信息 iOS设备之间蓝牙数据传输 从iOS7开始过期 局域网游 ...
- iOS绘图框架CoreGraphics分析
由于CoreGraphics框架有太多的API,对于初次接触或者对该框架不是十分了解的人,在绘图时,对API的选择会感到有些迷茫,甚至会觉得iOS的图形绘制有些繁琐.因此,本文主要介绍一下iOS的绘图 ...
- iOS绘图系统UIKit与Core Graphics
概述 iOS主要的绘图系统有UIKit,Core Graphics,Core Animation,Core Image,Open GL等,本片博文主要介绍UIKit与Core Graphics的绘图系 ...
- 论文第4章:iOS绘图平台的实现
面向移动设备的矢量绘图平台设计与实现 Design and Implementation of Mobile Device-oriented Vector Drawing Platform 引用本论文 ...
- IOS绘图
#import "ViewController.h" #import "DrawView.h" @interface ViewController () @pr ...
- iOS CoreData 介绍和使用(以及一些注意事项)
iOS CoreData介绍和使用(以及一些注意事项) 最近花了一点时间整理了一下CoreData,对于经常使用SQLite的我来说,用这个真的有点用不惯,个人觉得实在是没发现什么亮点,不喜勿喷啊.不 ...
- iOS CoreData介绍和使用(以及一些注意事项)
iOS CoreData介绍和使用(以及一些注意事项) 最近花了一点时间整理了一下CoreData,对于经常使用SQLite的我来说,用这个真的有点用不惯,个人觉得实在是没发现什么亮点,不喜勿喷啊.不 ...
- python 绘图介绍
1. python 绘图介绍 2. 函数 import numpy as np import matplotlib.pyplot as plt t = np.arange(0.0, 3.0, 0.01 ...
随机推荐
- Installing MySQL Server
Installing MySQL Server Here we will learn how to Compile and Install the MySQL Server from source c ...
- awk 多分隔符
#!/bin/bash log_path="./log/" dates=`date -d '-1 days' +'%Y%m%d'` cd $log_path; for i in ` ...
- How to install Pygame for Python 3.4 on Ubuntu 14.04(转)
First run this to install dependencies: sudo apt-get install mercurial python3-dev python3-numpy \ l ...
- ios开发之C语言第一天
最近在学习ios开发,先学习C语言,再学习OC和swift.正所谓"万丈高楼平地起",打好基础是很重要的,所以C语言也必须好好学习.学习中所使用的操作系统是OS X,开发工具是Xc ...
- Codeforces 446-C DZY Loves Fibonacci Numbers 同余 线段树 斐波那契数列
C. DZY Loves Fibonacci Numbers time limit per test 4 seconds memory limit per test 256 megabytes inp ...
- 安卓天天练练(四)drawable state 属性
今天又作茧自缚(item 写成 itme ╮(╯▽╰)╭ elipse还自动闭合了标签,来回查查查看报错,为啥点击无效呢!) 真欠抽,怪不得上班地铁上被个sb踢到脚趾头(目测有可能是同家公司的..同站 ...
- The Glorious Karlutka River =)
sgu438:http://acm.sgu.ru/problem.php?contest=0&problem=438 题意:有一条东西向流淌的河,宽为 W,河中有 N 块石头,每块石头的坐标( ...
- input text 不可编辑的解决办法
<div class="inp bg"> <input id="inp-query" type="text" name=& ...
- luoguP2266 爱的距离
题目:http://www.luogu.org/problem/show?pid=2266 题解:感觉题意不清,就去瞅题解了T_T 然后发现好水... 类似于MST,我们把边从小到大加进去就可以了. ...
- ResponseHelper
using System; using System.Collections.Generic; using System.Linq; using System.Web; using Cemetery_ ...