CGContextRef詳解

CGContextRef詳解   


Graphics Context是圖形上下文,可以將其理解爲一塊畫布,我們可以在上面進行繪畫操作,繪製完成後,將畫布放到我們的view中顯示即可,view看作是一個畫框.
Quartz提供了以下幾種類型的Graphics Context:
Bitmap Graphics Context
PDF Graphics Context
Window Graphics Context
Layer Context
Post Graphics Context
先熟悉下CGContextRef


Functions by Task--按接口功能分


1、Managing Graphics Contexts--管理圖形上下文
CGContextFlush       強制所有掛起的繪圖操作在一個窗口上下文中立即被渲染到目標設備。
CGContextGetTypeID   返回Quartz圖形上下文的類型標識符。
CGContextRelease     圖形上下文的引用計數-1。
CGContextRetain      圖形上下文的引用計數+1。
CGContextSynchronize 將一個窗口的圖像上下文內容更新,即所有的繪圖操作都會在下次同步到窗口上


2、Saving and Restoring the Current Graphics State--保存和恢復當前圖形狀態
CGContextSaveGState 將當前圖形狀態的副本PUSH到圖形狀態棧中;
  每個圖形上下文維護一個保存圖形狀態的堆棧。需要注意的是,注意,當前繪圖環境的所有方面都是圖形狀態的元素。比如當前路徑的圖像狀態是不考慮的,所以當調用這
個函數的時候是不保存的。保存的圖像狀態如下:
  CTM (current transformation matrix)
  clip region
  image interpolation quality
  line width
  line join
  miter limit
  line cap
  line dash
  flatness
  should anti-alias
  rendering intent
  fill color space
  stroke color space
  fill color
  stroke color
  alpha value
  font
  font size
  character spacing
  text drawing mode
  shadow parameters
  the pattern phase
  the font smoothing parameter
  blend mode
CGContextRestoreGState 將當前圖形狀態設置爲最近一次保存的狀態,即恢復狀態。


3、Getting and Setting Graphics State Parameters--獲取和設置圖形狀態參數
CGContextGetInterpolationQuality  返回當前的圖形上下文的插值(插值(Interpolation)是在不天生像素的環境下增長圖像像素大小的一種方法,在周圍像素色彩
的根蒂根基上用算術公式計算亡失像素的色彩。)質量水平。
CGContextSetFlatness              設置彎曲的路徑中的圖形上下文的準確性。
CGContextSetInterpolationQuality  設置圖形上下文的插值質量水平。
CGContextSetLineCap               圖形環境中的畫線的端點的樣式設置。
CGContextSetLineDash              設置圖形上下文中的虛線的模式。
CGContextSetLineJoin              設置圖像上下文中的接接線的樣式。
CGContextSetLineWidth             設置圖像上下文中的線的寬度。
CGContextSetMiterLimit            設置圖像上下文中的連接線的斜接限制。
CGContextSetPatternPhase          設置一個上下文的段落模式。
CGContextSetFillPattern           在指定的圖形上下文設置的填充圖案模式。
CGContextSetRenderingIntent       在當前圖形狀態設置渲染意向。
CGContextSetShouldAntialias       設置圖形上下文的抗鋸齒開啓或關閉。
CGContextSetStrokePattern         在指定的圖形上下文設置描邊圖案。
CGContextSetBlendMode
CGContextSetAllowsAntialiasing
CGContextSetAllowsFontSmoothing
CGContextSetShouldSmoothFonts
CGContextSetAllowsFontSubpixelPositioning
CGContextSetShouldSubpixelPositionFonts
CGContextSetAllowsFontSubpixelQuantization
CGContextSetShouldSubpixelQuantizeFonts


4、Constructing Paths--構建路徑
These functions are used to define the geometry of the current path. For more information on how paths are defined, see CGPath Reference.
這些函數是用來定義路徑的幾何形狀。對於如何定義的路徑的更多信息,請參閱CGPath參考。
CGContextAddArc
CGContextAddArcToPoint
CGContextAddCurveToPoint
CGContextAddLines
CGContextAddLineToPoint
CGContextAddPath
CGContextCopyPath
CGContextAddQuadCurveToPoint
CGContextAddRect
CGContextAddRects
CGContextBeginPath
CGContextClosePath
CGContextMoveToPoint
CGContextAddEllipseInRect


5、Painting Paths--繪畫路徑
These functions are used to stroke along or fill in the current path.
這些功能是用於邊緣或填充在當前路徑。
CGContextClearRect
CGContextDrawPath
CGContextEOFillPath
CGContextFillPath
CGContextFillRect
CGContextFillRects
CGContextFillEllipseInRect
CGContextStrokePath
CGContextStrokeRect
CGContextStrokeRectWithWidth
CGContextReplacePathWithStrokedPath
CGContextStrokeEllipseInRect
CGContextStrokeLineSegments


6、Getting Information About Paths--獲取關於路徑的信息
CGContextIsPathEmpty            表示目前的路徑是否包含任何的子路徑。
CGContextGetPathCurrentPoint    返回一個非空的路徑中的當前點。
CGContextGetPathBoundingBox     返回包含當前路徑的最小矩形。
CGContextPathContainsPoint      檢查當前路徑中是否包含指定的點。


7、Modifying Clipping Paths--修改剪貼路徑
CGContextClip                 修改當前剪貼路徑,使用非零繞數規則。
CGContextEOClip               修改當前剪貼路徑,使用奇偶規則。
CGContextClipToRect
CGContextClipToRects
CGContextGetClipBoundingBox
CGContextClipToMask


8、Setting Color, Color Space, and Shadow Values--設置顏色,色彩空間和陰影值
CGContextSetAlpha
CGContextSetCMYKFillColor
CGContextSetFillColor
CGContextSetCMYKStrokeColor
CGContextSetFillColorSpace
CGContextSetFillColorWithColor
CGContextSetGrayFillColor
CGContextSetGrayStrokeColor
CGContextSetRGBFillColor
CGContextSetRGBStrokeColor
CGContextSetShadow
CGContextSetShadowWithColor
CGContextSetStrokeColor
CGContextSetStrokeColorSpace
CGContextSetStrokeColorWithColor


9、Transforming User Space--轉換用戶空間
These functions allow you to examine and change the current transformation matrix (CTM) in a graphics context.
這些功能允許你檢查和更改圖形上下文的當前轉換矩陣(CTM)。
CGContextConcatCTM
CGContextGetCTM
CGContextRotateCTM
CGContextScaleCTM
CGContextTranslateCTM


10、Using Transparency Layers--使用透明圖層
CGContextBeginTransparencyLayer          開始一個透明層。
  直到相應的調用CGContextEndTransparencyLayer,在指定範圍內的所有後續繪製操作組合到一個完全透明的背景(它被視爲一個單獨的目標緩衝區從上下文)。
調用CGContextEndTransparencyLayer,使用全局alpha和陰影的上下文狀態合成到上下文中。
CGContextBeginTransparencyLayerWithRect  開始透明度層,它的邊界是指定的矩形,其內容是有界的。
CGContextEndTransparencyLayer            結束一個透明層。


11、Drawing an Image to a Graphics Context--繪製圖像圖形上下文
CGContextDrawTiledImage 重複繪製的圖像,擴展到提供的矩形,填補當前剪輯區域。
CGContextDrawImage      繪製圖像到圖形上下文中。


12、Drawing PDF Content to a Graphics Context--繪製一個圖形上下文的PDF內容
CGContextDrawPDFPage    繪製一個PDF頁面到當前的用戶空間。


13、Drawing With a Gradient--製圖漸變
CGContextDrawLinearGradient  繪製一個漸變填充定義的出發點和落腳點沿線變化。
CGContextDrawRadialGradient  繪製一個沿着由所提供的開始和結束的圓限定的區域變化的漸變填充。


14、Drawing With a Shading--製圖底紋
CGContextDrawShading  使用指定的陰影的背景,填充剪切路徑。


15、Setting Up a Page-Based Graphics Context--建立一個基於頁面的圖形上下文
CGContextBeginPage    基於頁面的圖形上下文中開始了新的一頁。
CGContextEndPage      在基於頁面的圖形上下文結束當前的頁面。


16、Drawing Glyphs--繪製字形
CGContextShowGlyphs             在當前文本位置顯示一個數組的字形。
CGContextShowGlyphsAtPoint      在指定的位置顯示一個數組的字形。
CGContextShowGlyphsWithAdvances 繪製具有不同的偏移量的一個數組字形。
CGContextShowGlyphsAtPositions  在所提供的位置繪製字形。


17、Drawing Text--繪製文本
CGContextGetTextMatrix        返回當前文本矩陣。
CGContextGetTextPosition      返回在繪製文本的位置。
CGContextSelectFont           設置在一個圖形上下文的字體和字體大小。
CGContextSetCharacterSpacing  設置當前字符間距。
CGContextSetFont              設置上下文的字體。
CGContextSetFontSize          設置上下文的字體大小。
CGContextSetTextDrawingMode   設置當前文本的繪圖模式。
CGContextSetTextMatrix        設置當前文本矩陣。
CGContextSetTextPosition      設置要繪製文本的位置。
CGContextShowText             在當前文本位置,由目前的文本矩陣指定的點顯示一個字符數組。
CGContextShowTextAtPoint      在指定的位置顯示一個字符串。


18、Converting Between Device Space and User Space--設備空間和用戶空間之間的轉換
CGContextGetUserSpaceToDeviceSpaceTransform
CGContextConvertPointToDeviceSpace
CGContextConvertPointToUserSpace
CGContextConvertSizeToDeviceSpace
CGContextConvertSizeToUserSpace
CGContextConvertRectToDeviceSpace
CGContextConvertRectToUserSpace












CGContextRef用法


1、簡介Quartz。


Quartz 是主要的描畫接口,支持基於路徑的描畫、抗鋸齒渲染、漸變填充模式、圖像、顏色、座標空間變換、以及PDF 文檔的創建、顯示、和分析。UIKit 爲Quartz 的圖像和顏色操作提供了Objective-C 的封裝。Core Animation 爲很多UIKit 的視圖屬性聲明的動畫效果提供底層支持,也可以用於實現定製的動畫。


在調用您提供的drawRect:方法之前,視圖對象會自動配置其描畫環境,使您的代碼可以立即進行描畫。作爲這些配置的一部分,UIView 對象會爲當前描畫環境創建一個圖形上下文(對應於CGContextRef 封裝類型)


用戶座標空間是您發出的所有描畫命令的工作環境。該空間的單位由點來表示。設備座標空間指的是設備內在的座標空間,由像素來表示。缺省情況下,用戶座標空間上的一個點等於設備座標空間的一個像素,這意味着一個點等於1/160英寸。然而,您不應該假定這個比例總是1:1。
UIColor 對象提供了一些便利方法,用於通過RGB、HSB、和灰度值指定顏色值。
您也可以使用Core Graphics 框架中的CGContextSetRGBStrokeColor 和CGContextSetRGBFillColor 函數來創建和設置顏色。
路徑輪廓可以用像CGContextStrokePath 這樣的函數來畫,即用當前的筆劃顏色畫出以路徑爲中心位置的線。路徑的填充則可以用CGContextFillPath 函數來實現,它的功能是用當前的填充顏色或樣式填充路徑線段包圍的區域。
獲取上下文,圖形上下文是什麼意思?
CGContextRef context = UIGraphicsGetCurrentContext();


2、簡單的示例代碼


[cpp] view plaincopy
// 畫一個正方形圖形 沒有邊框  
CGContextSetRGBFillColor(context, 0, 0.25, 0, 0.5);  
CGContextFillRect(context, CGRectMake(2, 2, 270, 270));  
CGContextStrokePath(context);  
  
// 寫文字  
CGContextSetLineWidth(context, 1.0);  
CGContextSetRGBFillColor (context,  1, 1, 1, 1.0);  
UIFont  *font = [UIFont boldSystemFontOfSize:11.0];  
[@"fangyp" drawInRect:CGRectMake(40, 40, 80, 20) withFont:font];  
  
// 畫一條線  
CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.5);//線條顏色  
CGContextMoveToPoint(context, 20, 20);  
CGContextAddLineToPoint(context, 200,20);  
CGContextStrokePath(context);  
  
// 畫正方形邊框  
CGContextSetRGBStrokeColor(context, 1, 1.0, 1.0, 1.0);  
CGContextSetLineWidth(context, 2.0);  
CGContextAddRect(context, CGRectMake(2, 2, 270, 270));  
CGContextStrokePath(context);  
  
// 畫方形背景顏色  
CGContextTranslateCTM(ctx, 0.0f, self.view.bounds.size.height);  
CGContextScaleCTM(ctx, 1.0f, -1.0f);  
UIGraphicsPushContext(ctx);  
CGContextSetLineWidth(ctx,320);  
CGContextSetRGBStrokeColor(ctx, 250.0/255, 250.0/255, 210.0/255, 1.0);  
CGContextStrokeRect(ctx, CGRectMake(0, 0, 320, 460));  
UIGraphicsPopContext(); 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章