UIKit Graphics System---Graphics and drawing in iOS 学习笔记1

UIKit类下的所有东西都不是线形安全的,需要放在主线程中。
  The View Drawing Cycle
  UIKit主要涉及的是更新UIView对象的内容,采用UIView自带的代码drawRect:。需要注意的是,该函数仅仅在UIView第一次被加载的时候调用一次,用于画该view中所有的可见部分。在其它时候,仅仅是画view中需要更新的那部分。可以触发redraw的情况如下:
  1、移动或者去除当前部分覆盖在该view上的其它view时
  2、将之前的一个隐藏的view显现出来
  3、把一个view从本view上scroll来回时
  4、调用setNeedsDisplay和setNeedsDisplayInRect:两个方法时
Coordinates and Coordinate Transforms

  UIKit的view的坐标原点位于view的左上角,坐标轴分别向下和向右延伸。CTM{currrent transformation matrix}便是一个将当前设备屏幕的坐标系和画出图形的坐标系相关联的一个东西。当view的drawRect:函数被调用的时候,CTM便被设置为将两者统一。但是可以通过修改CTM的scaling,ration,translation因子来改变图形的大小,方向,位置等。
  比如在(20,20)处一个图形,想让他移动到(10,10),通过CTM可以很容易实现。如果当初图形是通过path实现的,那么这么一个位置的改变,将是非常麻烦的。两种方法修改CTM,一种是通过CGContext类中的方法,另一种是通过CGAffineTransform。具体可以参考  Quartz 2D Programming Guide  and  CGAffineTransform Reference .
Graphics Context
  在调用drawRect:函数进行画图之前,需要进行一些配置。最开始,UIView的对象需要创建一个context,它是作为当前真正的作图环境的。设置context包括设置颜色,clipping area(这个可以参考developer网站中的mask部分),线寛,字体信息等。
  你可以随意的创建自己的context,而不一定是当前view的。通过这个你可以用于捕捉玩家的一些操作或者是根据操作创建一个图片或者pdf文档。具体可以参考: CGBitmapContextCreate  or  CGPDFContextCreate  。
Default Coordinate Systems and Drawing in iOS
  在iOS中,不同的framework可以有不同的坐标系,这一点需要注意。
  1、ULO(upper-left-origin),左上角 如 UIKit和Core Animation框架
  2、LLO(lower-left-origin),左下角 如 Core Graphics框架
    UIKit <wbr>Graphics <wbr>System---Graphics <wbr>and <wbr>drawing <wbr>in <wbr>iOS <wbr>学习笔记1
Drawing to the Screen
UIKit采用的是ULO,而Core Graphics采用的是LLO,如果希望用Core Graphics的函数在View中画图的话,必须采用ULO,另外两个框架的函数区分可以通过函数的开头字母了解,比如UIGraphicsGetCurrentCont ext()和CGContextRef。
  针对UIGraphicsContext函数的返回值,在pdf文档中返回一个pdf context,在图片的文档中返回一个图片的context。
Drawing to Bitmap Context and PDF Context

Bitmap context:

PDF context:

在iOS中,往bitmap context和PDF context中画东西的时候,建议采用UIKit的函数,但是如果采用Core Graphics的函数的时候,需要注意坐标的问题。
Drawing Paths
  针对path,两个框架UIKit和Core Graphics都有对应的API。UIKit'采用的是UIBezierPath,而Core Graphics采用的是CGPathRef。
  虽然两个框架都可以在context中创建path,但是对于iOS,推荐采用UIKit的UIBezierPath,除非需要采用Core Graphics框架的特殊功能。
  另外,如果生成线和context的坐标系不同的话,需要注意,结果和预期是不会一样的。
Flipping the Default Coordinate System
  在两个框架混合使用的时候,需要对坐标系进行调整才能获得需要的结果。这时候就需要使用CTM,对坐标系进行修改。
  比如,将一个Core Graphics的东西,画在一个view中,可以通过下面两个步骤完成CTM的改变:
  1、修改坐标远点
  2、将Y轴方向改为向下
  代码如下:
CGContextTranslateCTM(graphicsContext, 0.0, drawingRect.size.height);
CGContextScaleCTM(graphicsContext, 1.0, -1.0);
之后便可以将此context中的东西画在一个view中。 
Side Effects of Drawing with Different Coordinate Systems
此部分略过········· 下面开始新的内容。
Color 和 Color Spaces

The UIColor object provides convenience methods for specifying color values using RGB, HSB, and grayscale values. When creating colors in this way, you never need to specify the color space. It is determined for you automatically by the UIColor object.

You can also use the CGContextSetRGBStrokeColor and CGContextSetRGBFillColor functions in the Core Graphics framework to create and set colors. Although the Core Graphics framework includes support for creating colors using other color spaces, and for creating custom color spaces, using those colors in your drawing code is not recommended. Your drawing code should always use RGB colors.

这部分就不翻译了,非常简单易懂。

Improving Drawing Performance
画画面在任何的平台上都是非常昂贵的,合理的组织自己的代码是十分重要的。下面是一些建议:
  1、Drawing minimally
  在每一个循环周期中,不要试图更新整个画图区域,尽量只更新实际变化了的区域。例如:如果你采用UIView的drawRect:函数进行画图,那么一定要传递正确的参数,用以显示更新的范围。
  2、Mark opaque views as such
  构造一个不透明的内容比构造半透明的开销少很多。通过让view的content不包含任何的透明元素以及设置view.opaque = YES
  3、Resuse table cells and views during scrolling
  在滑动的过程中创建view的开销非常大,耗时非常长,这部分耗时,可以导致更新屏幕的时间减少,从而导致了不平衡的滑动,给人卡的感觉
  4、Avoid clearing the previous content during scrolling
  默认情况下,UIKit首先会情况context的buffer,之后才会调用drawRect:函数。在滑动的时候,多次清空buffer开销很大。这一点可以通过设置view.clearsContextBeforeDrawi ng = NO
  5、Minimize graphics state changes while drawing
  在view中修改图片的状态也是开销很大的操作。如果你需要画一些状态信息相同的东西,那么试图把他们放在一起画,可以减少需要修改的状态。
Drawing With Quartz and UIKit
  Quartz是iOS中native window server和drawing technology的general name。Core Graphics框架是Quartz的核心。该框架提供了一系列的函数方便了绘图。具体可以参考:  UIKit Framework Reference .
Configuring The Graphics Context
  绘图肯定是需要绘在context中的,在调用drawRect:的时候,系统已经创建了对应的CGContextRef类型的数据,可以通过UIGraphicsGetCurrentCont ext:函数获得此对象。参考:  CGContext Reference
Table 1-2    Core graphics functions for modifying graphics state

Graphics state

Core Graphics functions

UIKit alternatives

Current transformation matrix (CTM)

CGContextRotateCTM

CGContextScaleCTM

CGContextTranslateCTM

CGContextConcatCTM

None

Clipping area

CGContextClipToRect

None

Line: Width, join, cap, dash, miter limit

CGContextSetLineWidth

CGContextSetLineJoin

CGContextSetLineCap

CGContextSetLineDash

CGContextSetMiterLimit

None

Accuracy of curve estimation

CGContextSetFlatness

None

Anti-aliasing setting

CGContextSetAllowsAntialiasing

None

Color: Fill and stroke settings

CGContextSetRGBFillColor

CGContextSetRGBStrokeColor

UIColor class

Alpha global value (transparency)

CGContextSetAlpha

None

Rendering intent

CGContextSetRenderingIntent

None

Color space: Fill and stroke settings

CGContextSetFillColorSpace

CGContextSetStrokeColorSpace

UIColor class

Text: Font, font size, character spacing, text drawing mode

CGContextSetFont

CGContextSetFontSize

CGContextSetCharacterSpacing

UIFont class

Blend mode

CGContextSetBlendMode

The UIImage class and various drawing functions let you specify which blend mode to use.

  graphics context包含一个堆用于保存graphics states。当用Quartz创建一个新的context的时候,它的堆是空的。通过方法CGContextSaveGstate,将当前context的当前states放在堆上。之后对当前context的states进行修改不会影响这个copy,更改之后可以通过CGContextRestoreGState函数返回之前的context状态。
  通过对这个stack的操作,可以让context非常轻松的往返于之前的状态,或者undo上一次对状态的修改。通过这也是在cliping 一个context之后返回之前状态的唯一方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值