Quartz 2D 之Core Graphics Layer Drawing

CGLayer对象允许使用layers进行绘制。

适合使用layer绘制的场景:

1.高质量的离屏渲染重用。绘制到layer上的时候不需要知道颜色空间和设备独立信息。

2.重复绘制。如图12-1.当你重复绘制包括CGPath,CGShading,CGPDFPage对象时,绘制到layer会提高执行效率。layer不止适用于离屏渲染,比如PDF graphics context。

Quartz 2D 之Core Graphics Layer Drawing_第1张图片

3.缓存。当你绘制在缓存时,使用layer代替位图上下文。

How Layer Drawing Works layer如何工作

layer  ,CGLayerRef格式,被设计为最佳性能。For this reason a layer is typically a better choice for offscreen drawing than a bitmap graphics context is.

所有的Quartz都是绘制在上下文中的,layer也不例外。看一副layer 绘制的工序图:

Quartz 2D 之Core Graphics Layer Drawing_第2张图片

开始,你需要通过调用方法CGLayerCreateWithContext.创建一个CGLayer对象。本文中使用的是一个窗口上下文。Quartz创建一个layer的时候就已经获得了上下文的所有特性---分辨率,颜色空间和图形状态设置等等。如果不想使用图形上下文的大小,可是设置你想要的。

绘制layer之前 你需要获得与layer 有联系的上下文,调用方法CGLayerGetContext.CGLayer上下文被CPU缓存起来。

当你准备好layer的内容的时候,你调用方法CGContextDrawLayerInRectorCGContextDrawLayerAtPoint,绘制layer到图形上下文。

注意点:使用透明层实现阴影组团效果。使用CGLayer实现离屏渲染或者重复绘制。

Drawing with a Layer

步骤:

1.Create a CGLayer Object Initialized with an Existing Graphics Context

2.Get a Graphics Context for the Layer

3.Draw to the CGLayer Graphics Context

4.Draw the Layer to the Destination Graphics Context

SeeExample: Using Multiple CGLayer Objects to Draw a Flagfor a detailed code example.

1、调用方法CGLayerCreateWithContext传入三个参数,实例化layer。:上下文,size,一个辅助字典。

2、调用方法 CGLayerGetContext 或者layer的上下文。

3、绘制the CGLayer Graphics Context 。调用方法GContextFillRect,比如:CGContextFillRect (myLayerContext, myRect)

4.绘制layer 到目的上下文。CGContextDrawLayerInRect/GContextDrawLayerAtPoint,

示例:美国国旗



Quartz 2D 之Core Graphics Layer Drawing_第3张图片

简单分析:主要分为三块,红色与白色相间,蓝色矩形框,50个星星。第一块,第三块都可是使用layer 重复绘制。

代码片段:

Quartz 2D 之Core Graphics Layer Drawing_第4张图片
Quartz 2D 之Core Graphics Layer Drawing_第5张图片


Quartz 2D 之Core Graphics Layer Drawing_第6张图片
Quartz 2D 之Core Graphics Layer Drawing_第7张图片

demo地址:美国国旗 

你可能感兴趣的:(Quartz 2D 之Core Graphics Layer Drawing)