iphone中
主要通过下面的几个技术来绘图
OpenGL, Quartz, UIKit, or Core Animation
UIKit 是非线程安全的,所以最好把所有的绘图都放在主线程上执行
不管使用的哪个技术来绘图,所有的绘图都是在 UIView object 中进行, view决定绘图在那里进行
绘画周期
当一个view需要更新某一部分内容的时候,view会请求 drawRect: 方法
在view第一次请求drawRect方法的时候,传递的rectangle 参数一般是view的整个rectangle ,后续更新的时候,传递的一般是
需要更新的那部分rectangle
在几种情况下,view会重新绘图
1。移动或者移除另外一个view
2。设置view的hidden 属性为NO, view重新出现
3。滚动view,当滚出或者滚进来的时候
4。明确的请求setNeedsDisplay和setNeedsDisplayInRect:方法
当请求了一个 drawRect:方法,view会标志自己已经被更新了,然后等待下一个更新请求的到达
坐标系统
current transformation matrix (CTM)
默认坐标原点是左上角
如果需要改变坐标系统
有两种方法
1。 CGContext Reference :http://developer.apple.com/iphone/library/documentation/GraphicsImaging/Reference/CGContext/Reference/reference.html#//apple_ref/doc/uid/TP30000950
2。 CGAffineTransform :http://developer.apple.com/iphone/library/documentation/GraphicsImaging/Reference/CGAffineTransform/Reference/reference.html#//apple_ref/c/tdef/CGAffineTransform
图形上下文(Graphics Contexts)
当请求drawRect:方法 ,view object会自动配置图形环境,作为环境的一部分,uiview会创建一个图形上下文(a CGContextRef opaque type)
图形上下文定义基本图形属性,如颜色,剪切区域,线的宽度和样式信息,字体信息,合成选项,等等。
也可以自己创建图形上下文用 CGBitmapContextCreate 或者 CGPDFContextCreate 函数
需要注意的是,自己创建的图形上下文的原点是在左下角
CGContextSetRGBStrokeColor and CGContextSetRGBFillColor两个函数设置当前的笔锋色和填充色.
iphone支持的图形格式
.png
.tiff, .tif
.jpeg, .jpg
.gif
.bmp, .BMPf
.ico
.cur
.xbm
绘画技巧
1。部分更新: 假如在 drawRect: 中,更新rectangle 中的部分
2。如果一个view中没有透明部分,那么把 opaque 属性设置为 YES,这样会省很多的cpu
3。如果一张png图片没有任何透明的部分,那么久删除alpha通道,这样渲染的时候会省很多功夫
4。滚动的时候重用table cells和views
5。正常情况下,在view请求 drawRect: 之前都会清除current context buffer,来更新相同区域.如果在滚动的时候,反复的清除,
很浪费时间,这样的话就把view的clearsContextBeforeDrawing 设置成NO.
6。在绘图的时候,尽量少的图形状态改变.因为改变绘图状态需要window的server
提高图片质量
1。首选png图片格式
2。使用图片的时候,尽量的不要去改变大小,假如需要使用这个图片在很多地方,那么尽量使用和他们比较接近的图片大小的图片
Quartz 是Core Graphics的心脏, 主要提供以下东西
Graphics contexts
Paths
Images and bitmaps
Transparency layers
Colors, pattern colors, and color spaces
Gradients and shadings
Fonts
PDF content
更 多的详细内容在:http://developer.apple.com/iphone/library/documentation /CoreGraphics/Reference/CoreGraphics_Framework/index.html#//apple_ref/doc/uid/TP40007127
UIKit 是在Quartz的基本功能上的封装.他主要提供以下类
1。UIImage
2。UIColor
3。UIFont
4。UIScreen
5。生成png或者jpeg,用UIImage表现出来的函数
6。画矩形,和剪裁绘图区域的函数
7。改变和获取当前的图形上下文
更 多的内容在:http://developer.apple.com/iphone/library/documentation/UIKit /Reference/UIKit_Framework/index.html#//apple_ref/doc/uid/TP40006955
配置图形上下文
在drawRect:中,view已经自动的为我们创建了图形上下文,我们可以通过函数UIGraphicsGetCurrentContext 获取.
图形上下文使用堆栈来保存图像状态,CGContextSaveGState函数保存当前图像状态
CGContextRestoreGState函数来回到前面的版本
图片的绘画和创建
下面的几个场景,最好使用下面的方法
1>当view中只有一张图片,那么使用UIImageView 来加载图片
2>用代码创建一张图片
两种方法,
1, 先用UIGraphicsBeginImageContext 创建一个基于图片的图形上下文
画好图形后,用UIGraphicsGetImageFromCurrentImageContext 函数,生成图片
画完,最后用UIGraphicsEndImageContext 关闭图形上下文
2。用CGBitmapContextCreate 创建图形上下文,
在上面画图片,用CGBitmapContextCreateImage 创建CGImageRef
最后用CGImageRef来创建 UIImage
3>把一张图片保存为jpg或者png
加载一张图片,然后用UIImageJPEGRepresentation 或者UIImagePNGRepresentation 函数获取加载的图片的NSData ,然后用
NSData生成png或者jpg
创建和绘制路径
一个路径是一个二维几何场景,
UIKit 中包含 UIRectFrame UIRectFill 这 两个方法来创建简单的路径,比如矩形.
Core Graphics中还包含了椭圆,等等.
CGContextBeginPath 来创建一个基于路径的图形上下文,然后开始创建路径
完事后,生成 CGPathRef 或者 CGMutablePathRef
最后用CGContextStrokePath 和CGContextFillPath 填充颜色.
参 考:http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/GraphicsandDrawing/GraphicsandDrawing.html#//apple_ref/doc/uid/TP40007072-CH10-SW7
iPhone图形开发绘图教程是本文要介绍的内容,介绍了很多关于绘图类的使用,先来看详细内容讲解。
1、绘图总结:
绘图前设置:
- CGContextSetRGBFillColor/CGContextSetFillColorWithColor //填充色
- CGContextSetRGBStrokeColor/CGContextSetStrokeColorWithColor //笔颜色
- CGContextSetLineWidth //线宽度
绘图后设置:
注: 画完图后,必须 先用CGContextStrokePath来描线,即形状,后用CGContextFillPath来填充形状内的颜色.
2.常见图形绘制:
- CGContextFillRect/CGContextFillRects
- CGContextFillEllipseInRect
- CGContextAddRect/CGContextAddRects
- CGContextAddEllipseInRect
- CGContextAddLines
- CGContextMoveToPoint
- CGContextAddLineToPoint
3.常见控制方法:
- CGContextSaveGState
- CGContextRestoreGState
4.创建内存图像context:
- CGBitmapContextCreate <-----CGContextRlease释放
- CGColorSpaceCreateWithName (KCGColorSpaceGenericRGB)
- CGColorSpaceRlease
- CGBitmapContextCreateImage() <-----CGImageRlease 释放.
- eg:
- CGContextRefMyCreateBitmapContext(intpixelsWide,intpixelsHigh)
- {
- CGContextRef context=NULL;
- CGColorSpaceRefcolorSpace;
- void* bitmapData;
- int bitmapByteCount;
- int bitmapBytesPerRow;
- bitmapBytesPerRow =(pixelsWide*4);
- bitmapByteCount =(bitmapBytesPerRow*pixelsHigh);
- colorSpace=CGColorSpaceCreateDeviceRGB();
- bitmapData=malloc(bitmapByteCount);
- if(bitmapData==NULL)
- {
- fprintf(stderr,"Memorynotallocated!");
- returnNULL;
- }
- context=CGBitmapContextCreate(bitmapData,
- pixelsWide, pixelsHigh, 8,
- bitmapBytesPerRow, colorSpace,
- kCGImageAlphaPremultipliedLast);
- if(context==NULL)
- {
- free(bitmapData);
- fprintf(stderr,"Contextnotcreated!");
- returnNULL;
- }
- CGColorSpaceRelease(colorSpace);
- returncontext;
- }
5.图形的变换:
- CGContextTranslateCTM
- CGContextRotateCTM
- CGContextScaleCTM
6.常用函数:
- CGRectContainsPoint();
- CGRectContainsRect();
- CGRectIntersectsRect();
- CGRectIntersection();
- CGPointEqualToPoint();
- CGSizeEqualToSize();
7.从原图片中取小图.
- CGImageCreateWithImageInRect
8.屏幕快照:
- #import "QuartzCore/QuartzCore.h"
- UIGraphicsBeginImageContext(yourView.frame.size);
- [[yourView layer] renderInContext:UIGraphicsGetCurrentContext()];
- UIImage*screenshot =UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- from:http://www.cppblog.com/zhangyuntaoshe/articles/123066.html
合并两张bit图到一张image的方法
- To graphically merge two images into a new image, you do something like this:
- UIImage *result = nil;
- unsignedchar *data = calloc(1,size.width*size.height*kBytesPerPixel);
- if (data != NULL) {
- // kCGImageAlphaPremultipliedLast 为预记录的#define value
- // 设置context上下文
- CGContextRef context = CGBitmapContextCreate(
- data, size.width, size.height, 8, size.width*kBytesPerPixel,
- CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedLast);
- if (context != NULL) {
- UIGraphicsPushContext(context);
- // Image 为下载的背景图片,用于比较context
- CGContextTranslateCTM(context, 0, size.height);
- CGContextScaleCTM(context, 1, -1);
- [image drawInRect:imageRect];
- [image2 drawInRect:image2Rect];
- UIGraphicsPopContext();
- CGImageRef imageRef = CGBitmapContextCreateImage(context);
- if (imageRef != NULL) {
- result = [UIImageimageWithCGImage:imageRef];
- CGImageRelease(imageRef);
- }
- CGContextRelease(context);
- }
- free(data);
- }
- return result;
关键方法:
- CGContextRef context = CGBitmapContextCreate();
- CGContextTranslateCTM();
- CGContextScaleCTM();
- CGImageRef imageRef = CGBitmapContextCreateImage(context);
- CGImageRelease(imageRef);
小结:iPhone图形开发绘图教程的内容介绍完了,希望本文对你有所帮助!
iphone 绘图
http://blog.csdn.net/yuhuangc/article/details/7066699