iPhone Application Programming Guide - Graphics and Drawing

来源:互联网 发布:怎样解析域名 编辑:程序博客网 时间:2024/05/01 20:18

【iPhone Application Programming Guide, Ch4】

【补充说明:部分细节有待验证】

iPhone自带的库:
quartz
core animation
UIKit

1. UIKit
所有对图形的调用都封装在UIView的实例中;

1.1 画图:
UIView drawRect
第一次画图时,将全图绘制,以后根据实际情况绘制。
有以下行为会触发view更新:
(1)其他的view掩盖当前view;
(2)将隐藏view显示;
(3)scrollling view,然后再返回
(4)显式调用setNeedsDisplay、setNeedsDisplayInRect:

如果要更改显示内容,必须使用方法(4)触发更新。

1.2 坐标系和坐标变换:
当前坐标系(CTM):定义在CGContext中
创建CGAffineTransform结构做坐标变换。

1.3 图形上下文:
画图前,view会自动的配置环境,创建图形上下文(CGContextRef)
包括信息:图像,区域,线的宽度,字体等等

可以创建自己的上下文(CGBitmapContextCreate  CGPDFContextCreate)
【使用自己的上下文时,坐标原点在左下角,而不是标准的左上角】

参考:Quartz 2D Programming Guide
Q:CGContext Reference是个什么东西?

1.3 points VS. pixels
Quartz绘图使用基于向量的绘图模型。
Quartz使用的绘图空间user coordinate space被系统映射到实际的像素点。
精确性考虑,绘图坐标使用浮点数,而不是整数。

device coordinate space:通过像素来表达。
通常情况下,一个point映射到一个pixels

1.4 Color and Color Spaces
推荐使用RGB Color

(UIColor, )
CGContextSetRGBStrokeColor
CGContextSetRGBFillColor

1.5 图像类型
推荐使用:PNG
支持的图像类型:png, tif, jpg, gif, bmp, ico, cur, xbm


2. Drawing Tips

2.1 何时使用自定义绘图
显示内容需要动态改变时使用,否则不推荐使用自定义绘图。

可以使用UIIMageView显示
(UILable)

2.2 Improving Drawing Performance
(1)Draw minimally
(2)Mark opaque views as such:设置view的opaque属性为yes
(3)Remove alpha channels from PNG files:
(4)Reuse table cells and views during scrolling
翻页时应当避免创建新的view,会影响展示。
(5)Avoid clearing the previous content during scrolling
默认:UIKit会清空一个view当前的上下文buffer,然后再调用view的drawRect:
对于翻页,应当设置view的clearsContextBeforeDrawing为NO
(6)Minimize graphics state changes while drawing

2.3 Maintaining Image Quality
建议使用PNG图像格式;
使用大小合适的图片而不是scale,如果需要展现一个图片的不同大小,建议对一个图片搞多个版本;

3 Drawing with Quartz and UIKit
Quartz:iPhone绘图功能的总称。
Core Graphics framework是quartz的核心部分和主要接口。
内容:
Graphics context
Paths
Images and bitmaps
Transparency layer
Colors, pattern colors, and color spaces
Gradients and shadings
fonts
PDF Content

UIKit的图形类:
(1)UIImage
(2)UIColor
(3)UIFont
(4)UIScreen:screen的基本信息

可参考:
Core Graphics framework reference

3.1 配置图形上下文
UIGraphicsGetCurrentContext:得到Graphics context()的引用
通过调用Core Graphics的函数,可以修改当前的图形状态(Graphics state)。

(1)当前转换矩阵(CTM,current transformation matrix)
CGContextRotateCTM
CGContextScaleCTM
CGContextTranslateCTM
CGContextConcatCTM
(2)剪贴区
CGContextClipToRect
(3)线
CGContextSetLineWidth
CGContextSetLineJoin
CGContextSetLineCap
CGContextSetLineDash
CGContextSetMiterLimit

(4)平滑性
CGContextSetFlatness
(5)抗锯齿设置
CGContextSetAllowsAntialiasing
(6)颜色
CGContextSetRGBFillColor
CGContextSetRGBStrokeColor
【在UIKit的层次,对颜色的操作,也可以通过UIColor来实现】
(7)透明(Alpha值)
CGContextSetAlpha
(8)色彩对应方式(Rendering intent)
CGContextSetRenderingIntent
(9)色彩空间(Color space)
CGContextSetFillColorSpace
CGContextSetStrokeColorSpace
(10)文本(字体,大小,etc)
CGContextSetFont
CGContextSetFontSize
CGContextSetCharacterSpacing
【在UIKit层次,用UIFount类也可以实现】
(11)混合模式(blend mode)
CGContextSetBlendMode
【UIImage也可以指定blend mode】


Graphic context包括了graphics状态的一个队列。
context创建时为空,调用CGContextSaveGState将当前的state推入队列。
完成自定义绘图后,可以通过CGContextRestoreGState恢复保存的state。


3.2 创建和绘制Image
iPhone可以通过UIKit或者Core Graphics 的frameworks载入和展现image。
建议使用UIKit类对图像进行操作。
(1)在view中作为内容展现  -- image是view
使用UIIMageView
(2)在view中作为装饰展现  -- image是view的一部分
使用UIImage
(3)将位图数据保存到image
UIKit的方式:
- 使用UIFraphicBeginImageContext创建一个image based图形上下文;
- 画图;
- 调用UIGraphicsGetImageFromCurrentImageContext来产生一个image;
- 调用UIGraphicsEndImageContext关闭图形上下文。

Core Graphics的方式:
- 使用CGBitmapContextCreate创建一个bitmap图形的上下文
- 画图;
- 使用CGBitmapContextCreateImage创建一个CGImageRef(?)
(4)image保存问jpeg/png
- 创建一个UIImage对象;
- 调用UIImageRepreseating/UIImagePNGRepresentation获取一个NSData的对象;
- 保存NSData到相应的文件格式;

绘图:
[view drawRect]
     [Image drawAtPoint:]

Tips: Quartz图像左边起始是在左下角,向右,向上是正方向。


3.3 创建和绘制Path
path:使用线和贝塞尔曲线描述的图形

UIKit对path的操作:
UIRectFrame
UIRectFill

Core Graphics对path的操作:<>

创建path:
- CGContextBeginPath配置图形上下文
- 画图
  - CGContextStrokePath:创建一个line
  - CGContextFillPath
- 创建CGPathRef/CGMutablePathRef来保存对path的引用


3.4 模式、渐变、阴影 (Patterns,Gradients and Shadings)
Core Graphic提供的功能,可以创建渐变色填充path

Pattern:repeating images or content
Gradients/Shading:提供颜色的渐变功能


4. Drawing with OpenGL ES
用于绘制高帧率图像,绘图在硬件完成,所以OpenGL绘图通常非常快。

iPhone支持:OpenGLES.framework

可参考:OpenGL ES Programming Guide for iPhone OS


5. Core Animation Effects
Core Animation: Object C的framework,用于创建流,实时动画。
本身不具备创建画图能力。

基本动画:【可参看page71,Animating views】

【可参看Core Animation Programming Guide】

5.1 Layer
Core Animation的核心技术。
Layer是类似于view的轻量级对象,但其实际上是model对象,封装了图形,时间和可视属性等内容。

三种方式处理Layer的展示:
(1)将一个CGImageRef赋值给layer对象的contents
(2)将一个delegate赋给一个layer对象并处理绘图
(3)继承CALayer并重载其展现方式;

对layer的操作实质上时对model层次数据的操作。

【??】

5.2 Animations
CAAnimation类

多个动画组合成一个transactiong。
CATransaction类

【自定义动画:可参考:Animation types and timing programming guide】

 

 

原创粉丝点击