原文:http://www.cnblogs.com/hanjun/archive/2012/11/25/2787682.html by:What_If
主要是3种方式,Core Animation、Core Graphic和OpenGL ES。
操作简易度:CA>CG>OpenGL
性能和功能度:OpenGL>CG>CA
1.Core Animation
非娱乐类的软件都会用到的动画,操作简单。
2.Quartz 2D绘图
是一个2D绘图引擎。
(1) 绘图Context是一个绘图的目标对象,定义了绘图的基本属性,如颜色、绘图范围、线宽及样式等。
(2)通过UIView会创建Context,可以用类似如下的语句来得到当前的Context.
CGContextRef currentContext = UIGraphicsGetCurrentContext();
(3)如果在对其进行修改前想要保存当前状态,可以使用UIGraphicsPushContext;
要恢复保存过的Context,则可用UIGraphicsPopContext。
(4)path,路径其实就是用一系列坐标点标识出来的一条曲线或折线,创建好之后就可以沿其画线,或者对封闭的空间进行填充。
通过一个简单的画板(白板)来熟悉 Quartz 2D绘图
添加一个Empty Application,新建一个UIView类
在AppDelegate中修改代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
// 创建View,并指定View的大小为全屏
WhiteBoardView *whiteView = [[WhiteBoardView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[self.window addSubview:whiteView]; // 把创建的View作为子视图加入窗口
[whiteView release];
[self.window makeKeyAndVisible];
return YES;
}
修改UIView类中代码:
// DrawingBoard
#import <UIKit/UIKit.h>
@interface WhiteBoardView : UIView{
CGContextRef whiteBoardContext;
CGLayerRef whiteBoardLayer;
}
@end
实现类:
// DrawingBoard
#import " WhiteBoardView.h "
@implementation WhiteBoardView
// 对进行重写,以便在视图初始化的时候创建并设置自定义的Context
- ( id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor whiteColor]; // 指定视图的背景色
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); // 指定色彩空间为RGB
// 创建Bitmap Context,大小为View自身的大小
whiteBoardContext = CGBitmapContextCreate(NULL, self.frame.size.width, self.frame.size.height, 8,
4 *self.frame.size.width, colorSpace, kCGImageAlphaPremultipliedFirst);
CGColorSpaceRelease(colorSpace) ;
// 创建新的CGLayer,用于画图
whiteBoardLayer = CGLayerCreateWithContext(whiteBoardContext, self.frame.size, NULL);
// 得到新建层的Context
CGContextRef layerContext = CGLayerGetContext(whiteBoardLayer);
// 指定新建层Context的线宽
CGContextSetLineWidth(layerContext, 1.5);
CGContextSetLineCap(layerContext, kCGLineCapRound); // 指定线头形状为圆形
CGContextSetRGBStrokeColor(layerContext, 0.0, 0.0, 0.0, 1); // 指定线的颜色,黑色
}
return self;
}
// 对drawRect进行重写
- ( void)drawRect:(CGRect)rect
{
// 先得到当前的Context
CGContextRef currentContext = UIGraphicsGetCurrentContext();
// 根据Context的内容生成Bitmap
CGImageRef image = CGBitmapContextCreateImage(whiteBoardContext);
// 把Bitmap绘制到Context上
CGContextDrawImage(currentContext, [self bounds], image);
// 把whiteBoardLayer也绘到当前Context中
CGContextDrawLayerInRect(currentContext , [self bounds], whiteBoardLayer);
}
// 屏幕触摸事件 触摸开始
-( void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event
{
UITouch *theTouch = [touches anyObject]; // 先得到touch对象
if ([theTouch tapCount] == 2) // 判断是否为双击,是就清空图像
{
CGContextClearRect(whiteBoardContext, [self bounds]); // 清空
[self setNeedsDisplay]; // 刷新屏幕显示
}
else // 如果不是双击,就按手指划动处理,结果是画出一个点
{
[self touchesMoved:touches withEvent: event];
}
}
// 手指划动时画线的代码
-( void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *) event
{
UITouch *theTouch = [touches anyObject];
// 得到当前位置
CGPoint currentTouchLocation = [theTouch locationInView:self];
// 得到上次位置
CGPoint lastTouchLoacation = [theTouch previousLocationInView:self];
// 取得Context
CGContextRef layerContext = CGLayerGetContext(whiteBoardLayer);
// 开始定义Path
CGContextBeginPath(layerContext);
// 把Path的起点移动到上次位置
CGContextMoveToPoint(layerContext, lastTouchLoacation.x, lastTouchLoacation.y);
// 在上次位置和当前位置之间连线,并记入Path
CGContextAddLineToPoint(layerContext, currentTouchLocation.x, currentTouchLocation.y);
// 沿Path画线
CGContextStrokePath(layerContext);
[self setNeedsDisplay]; // 刷新屏幕
}
- ( void)dealloc
{
CGContextRelease(whiteBoardContext);
CGLayerRelease(whiteBoardLayer);
[super dealloc];
}
@end
3.OpenGL ES编程
一般是三个步骤:
(1)在Context中绘图
(2)把Context中的内容送入framebuffer
(3)显示framebuffer