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. C语言实现五子棋简单功能

    /******************************************************************** C-4.29-1: 实现五子棋游戏 操作说明:用方向键或者& ...

  2. css的继承、层叠和特殊性

    1,继承  css的某些样式是具有继承性的,那么什么是继承呢?继承是一种规则,它允许样式不仅应用于某个特定html标签元素,而且应用于其后代. 但注意有一些css样式是不具有继承性的.如border: ...

  3. vs2015安装没有wim32

    刚开始在官网上下载VS2015没在意太多,选择了默认安装,结果是没有win64的,所以就不能写c代码.默认安装很多库都没有,所以要什么都得下载.转载一篇文章

  4. BZOJ 2754: [SCOI2012]喵星球上的点名

    2754: [SCOI2012]喵星球上的点名 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 649  Solved: 305[Submit][Sta ...

  5. [BZOJ 1502] [NOI2005] 月下柠檬树 【Simpson积分】

    题目链接: BZOJ - 1502 题目分析 这是我做的第一道 Simpson 积分的题目.Simpson 积分是一种用 (fl + 4*fmid + fr) / 6 * (r - l) 来拟合 fl ...

  6. navBar

    改变NavgationBar的颜色: [[UINavigationBar appearance] setBarTintColor:[UIColor blackColor]]; 改变NavgationB ...

  7. Ed Burns谈HTTP/2和Java EE Servlet 4规范

    在2015年JavaLand大会上,Ed Burns展示了Java EE Servlet 4.0规范(JSR 369)的概要,演讲的重点在于Java EE平台对HTTP/2的支持.HTTP/2旨在解决 ...

  8. How Many Shortest Path

    zoj2760:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2760 题意:给你一张有向带权图,然后问你最短路径有多少条. ...

  9. Bluetooth LE(低功耗蓝牙) - 第三部分

    回顾 在本系列的前两篇文章中,我们已经了解了一些关于Bluetooth LE的背景并建立一个简单的Activity / Service框架.   在这篇文章中,我们将探讨Bluetooth LE的细节 ...

  10. XAMPP下的composer的安装

    很多开源软件,都需要通过composer来安装,Composer 是 PHP 的一个依赖管理工具.它允许你申明项目所依赖的代码库,它会在你的项目中为你安装他们. 声明关系 比方说,你正在创建一个项目, ...