CGLayer對象允許使用layers進行繪制。
適合使用layer繪制的場景:
1.高質量的離屏渲染重用。繪制到layer上的時候不需要知道顏色空間和設備獨立信息。
2.重復繪制。如圖12-1.當你重復繪制包括CGPath,CGShading,CGPDFPage對象時,繪制到layer會提高執行效率。layer不止適用于離屏渲染,比如PDF graphics context。
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也不例外??匆桓眑ayer 繪制的工序圖:
開始,你需要通過調用方法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,
示例:美國國旗
簡單分析:主要分為三塊,紅色與白色相間,藍色矩形框,50個星星。第一塊,第三塊都可是使用layer 重復繪制。
代碼片段: