CGContextRef詳解

Graphics Context是圖形上下文,可以將其理解為一塊畫布,我們可以在上面進(jìn)行繪畫操作,繪制完成后,將畫布放到我們的view中顯示即可,view看作是一個(gè)畫框.Quartz提供了以下幾種類型的Graphics Context:Bitmap Graphics ContextPDF Graphics ContextWindow Graphics ContextLayer ContextPost Graphics Context先熟悉下CGContextRefFunctions by Task--按接口功能分1、Managing Graphics Contexts--管理圖形上下文CGContextFlush 強(qiáng)制所有掛起的繪圖操作在一個(gè)窗口上下文中立即被渲染到目標(biāo)設(shè)備。CGContextGetTypeID 返回Quartz圖形上下文的類型標(biāo)識符。CGContextRelease 圖形上下文的引用計(jì)數(shù)-1。CGContextRetain 圖形上下文的引用計(jì)數(shù)+1。CGContextSynchronize 將一個(gè)窗口的圖像上下文內(nèi)容更新,即所有的繪圖操作都會在下次同步到窗口上2、Saving and Restoring the Current Graphics State--保存和恢復(fù)當(dāng)前圖形狀態(tài)CGContextSaveGState 將當(dāng)前圖形狀態(tài)的副本PUSH到圖形狀態(tài)棧中; 每個(gè)圖形上下文維護(hù)一個(gè)保存圖形狀態(tài)的堆棧。需要注意的是,注意,當(dāng)前繪圖環(huán)境的所有方面都是圖形狀態(tài)的元素。比如當(dāng)前路徑的圖像狀態(tài)是不考慮的,所以當(dāng)調(diào)用這個(gè)函數(shù)的時(shí)候是不保存的。保存的圖像狀態(tài)如下: 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 modeCGContextRestoreGState 將當(dāng)前圖形狀態(tài)設(shè)置為最近一次保存的狀態(tài),即恢復(fù)狀態(tài)。3、Getting and Setting Graphics State Parameters--獲取和設(shè)置圖形狀態(tài)參數(shù)CGContextGetInterpolationQuality 返回當(dāng)前的圖形上下文的插值(插值(Interpolation)是在不天生像素的環(huán)境下增長圖像像素大小的一種方法,在周圍像素色彩的根蒂根基上用算術(shù)公式計(jì)算亡失像素的色彩。)質(zhì)量水平。CGContextSetFlatness 設(shè)置彎曲的路徑中的圖形上下文的準(zhǔn)確性。CGContextSetInterpolationQuality 設(shè)置圖形上下文的插值質(zhì)量水平。CGContextSetLineCap 圖形環(huán)境中的畫線的端點(diǎn)的樣式設(shè)置。CGContextSetLineDash 設(shè)置圖形上下文中的虛線的模式。CGContextSetLineJoin 設(shè)置圖像上下文中的接接線的樣式。CGContextSetLineWidth 設(shè)置圖像上下文中的線的寬度。CGContextSetMiterLimit 設(shè)置圖像上下文中的連接線的斜接限制。CGContextSetPatternPhase 設(shè)置一個(gè)上下文的段落模式。CGContextSetFillPattern 在指定的圖形上下文設(shè)置的填充圖案模式。CGContextSetRenderingIntent 在當(dāng)前圖形狀態(tài)設(shè)置渲染意向。CGContextSetShouldAntialias 設(shè)置圖形上下文的抗鋸齒開啟或關(guān)閉。CGContextSetStrokePattern 在指定的圖形上下文設(shè)置描邊圖案。CGContextSetBlendModeCGContextSetAllowsAntialiasingCGContextSetAllowsFontSmoothingCGContextSetShouldSmoothFontsCGContextSetAllowsFontSubpixelPositioningCGContextSetShouldSubpixelPositionFontsCGContextSetAllowsFontSubpixelQuantizationCGContextSetShouldSubpixelQuantizeFonts4、Constructing Paths--構(gòu)建路徑These functions are used to define the geometry of the current path. For more information on how paths are defined, see CGPath Reference.這些函數(shù)是用來定義路徑的幾何形狀。對于如何定義的路徑的更多信息,請參閱CGPath參考。CGContextAddArcCGContextAddArcToPointCGContextAddCurveToPointCGContextAddLinesCGContextAddLineToPointCGContextAddPathCGContextCopyPathCGContextAddQuadCurveToPointCGContextAddRectCGContextAddRectsCGContextBeginPathCGContextClosePathCGContextMoveToPointCGContextAddEllipseInRect5、Painting Paths--繪畫路徑These functions are used to stroke along or fill in the current path.這些功能是用于邊緣或填充在當(dāng)前路徑。CGContextClearRectCGContextDrawPathCGContextEOFillPathCGContextFillPathCGContextFillRectCGContextFillRectsCGContextFillEllipseInRectCGContextStrokePathCGContextStrokeRectCGContextStrokeRectWithWidthCGContextReplacePathWithStrokedPathCGContextStrokeEllipseInRectCGContextStrokeLineSegments6、Getting Information About Paths--獲取關(guān)于路徑的信息CGContextIsPathEmpty 表示目前的路徑是否包含任何的子路徑。CGContextGetPathCurrentPoint 返回一個(gè)非空的路徑中的當(dāng)前點(diǎn)。CGContextGetPathBoundingBox 返回包含當(dāng)前路徑的最小矩形。CGContextPathContainsPoint 檢查當(dāng)前路徑中是否包含指定的點(diǎn)。7、Modifying Clipping Paths--修改剪貼路徑CGContextClip 修改當(dāng)前剪貼路徑,使用非零繞數(shù)規(guī)則。CGContextEOClip 修改當(dāng)前剪貼路徑,使用奇偶規(guī)則。CGContextClipToRectCGContextClipToRectsCGContextGetClipBoundingBoxCGContextClipToMask8、Setting Color, Color Space, and Shadow Values--設(shè)置顏色,色彩空間和陰影值CGContextSetAlphaCGContextSetCMYKFillColorCGContextSetFillColorCGContextSetCMYKStrokeColorCGContextSetFillColorSpaceCGContextSetFillColorWithColorCGContextSetGrayFillColorCGContextSetGrayStrokeColorCGContextSetRGBFillColorCGContextSetRGBStrokeColorCGContextSetShadowCGContextSetShadowWithColorCGContextSetStrokeColorCGContextSetStrokeColorSpaceCGContextSetStrokeColorWithColor9、Transforming User Space--轉(zhuǎn)換用戶空間These functions allow you to examine and change the current transformation matrix (CTM) in a graphics context.這些功能允許你檢查和更改圖形上下文的當(dāng)前轉(zhuǎn)換矩陣(CTM)。CGContextConcatCTMCGContextGetCTMCGContextRotateCTMCGContextScaleCTMCGContextTranslateCTM10、Using Transparency Layers--使用透明圖層CGContextBeginTransparencyLayer 開始一個(gè)透明層。 直到相應(yīng)的調(diào)用CGContextEndTransparencyLayer,在指定范圍內(nèi)的所有后續(xù)繪制操作組合到一個(gè)完全透明的背景(它被視為一個(gè)單獨(dú)的目標(biāo)緩沖區(qū)從上下文)。調(diào)用CGContextEndTransparencyLayer,使用全局alpha和陰影的上下文狀態(tài)合成到上下文中。CGContextBeginTransparencyLayerWithRect 開始透明度層,它的邊界是指定的矩形,其內(nèi)容是有界的。CGContextEndTransparencyLayer 結(jié)束一個(gè)透明層。11、Drawing an Image to a Graphics Context--繪制圖像圖形上下文CGContextDrawTiledImage 重復(fù)繪制的圖像,擴(kuò)展到提供的矩形,填補(bǔ)當(dāng)前剪輯區(qū)域。CGContextDrawImage 繪制圖像到圖形上下文中。12、Drawing PDF Content to a Graphics Context--繪制一個(gè)圖形上下文的PDF內(nèi)容CGContextDrawPDFPage 繪制一個(gè)PDF頁面到當(dāng)前的用戶空間。13、Drawing With a Gradient--制圖漸變CGContextDrawLinearGradient 繪制一個(gè)漸變填充定義的出發(fā)點(diǎn)和落腳點(diǎn)沿線變化。CGContextDrawRadialGradient 繪制一個(gè)沿著由所提供的開始和結(jié)束的圓限定的區(qū)域變化的漸變填充。14、Drawing With a Shading--制圖底紋CGContextDrawShading 使用指定的陰影的背景,填充剪切路徑。15、Setting Up a Page-Based Graphics Context--建立一個(gè)基于頁面的圖形上下文CGContextBeginPage 基于頁面的圖形上下文中開始了新的一頁。CGContextEndPage 在基于頁面的圖形上下文結(jié)束當(dāng)前的頁面。16、Drawing Glyphs--繪制字形CGContextShowGlyphs 在當(dāng)前文本位置顯示一個(gè)數(shù)組的字形。CGContextShowGlyphsAtPoint 在指定的位置顯示一個(gè)數(shù)組的字形。CGContextShowGlyphsWithAdvances 繪制具有不同的偏移量的一個(gè)數(shù)組字形。CGContextShowGlyphsAtPositions 在所提供的位置繪制字形。17、Drawing Text--繪制文本CGContextGetTextMatrix 返回當(dāng)前文本矩陣。CGContextGetTextPosition 返回在繪制文本的位置。CGContextSelectFont 設(shè)置在一個(gè)圖形上下文的字體和字體大小。CGContextSetCharacterSpacing 設(shè)置當(dāng)前字符間距。CGContextSetFont 設(shè)置上下文的字體。CGContextSetFontSize 設(shè)置上下文的字體大小。CGContextSetTextDrawingMode 設(shè)置當(dāng)前文本的繪圖模式。CGContextSetTextMatrix 設(shè)置當(dāng)前文本矩陣。CGContextSetTextPosition 設(shè)置要繪制文本的位置。CGContextShowText 在當(dāng)前文本位置,由目前的文本矩陣指定的點(diǎn)顯示一個(gè)字符數(shù)組。CGContextShowTextAtPoint 在指定的位置顯示一個(gè)字符串。18、Converting Between Device Space and User Space--設(shè)備空間和用戶空間之間的轉(zhuǎn)換CGContextGetUserSpaceToDeviceSpaceTransformCGContextConvertPointToDeviceSpaceCGContextConvertPointToUserSpaceCGContextConvertSizeToDeviceSpaceCGContextConvertSizeToUserSpaceCGContextConvertRectToDeviceSpaceCGContextConvertRectToUserSpaceCGContextRef用法1、簡介Quartz。Quartz 是主要的描畫接口,支持基于路徑的描畫、抗鋸齒渲染、漸變填充模式、圖像、顏色、坐標(biāo)空間變換、以及PDF 文檔的創(chuàng)建、顯示、和分析。UIKit 為Quartz 的圖像和顏色操作提供了Objective-C 的封裝。Core Animation 為很多UIKit 的視圖屬性聲明的動(dòng)畫效果提供底層支持,也可以用于實(shí)現(xiàn)定制的動(dòng)畫。在調(diào)用您提供的drawRect:方法之前,視圖對象會自動(dòng)配置其描畫環(huán)境,使您的代碼可以立即進(jìn)行描畫。作為這些配置的一部分,UIView 對象會為當(dāng)前描畫環(huán)境創(chuàng)建一個(gè)圖形上下文(對應(yīng)于CGContextRef 封裝類型)用戶坐標(biāo)空間是您發(fā)出的所有描畫命令的工作環(huán)境。該空間的單位由點(diǎn)來表示。設(shè)備坐標(biāo)空間指的是設(shè)備內(nèi)在的坐標(biāo)空間,由像素來表示。缺省情況下,用戶坐標(biāo)空間上的一個(gè)點(diǎn)等于設(shè)備坐標(biāo)空間的一個(gè)像素,這意味著一個(gè)點(diǎn)等于1/160英寸。然而,您不應(yīng)該假定這個(gè)比例總是1:1。UIColor 對象提供了一些便利方法,用于通過RGB、HSB、和灰度值指定顏色值。您也可以使用Core Graphics 框架中的CGContextSetRGBStrokeColor 和CGContextSetRGBFillColor 函數(shù)來創(chuàng)建和設(shè)置顏色。路徑輪廓可以用像CGContextStrokePath 這樣的函數(shù)來畫,即用當(dāng)前的筆劃顏色畫出以路徑為中心位置的線。路徑的填充則可以用CGContextFillPath 函數(shù)來實(shí)現(xiàn),它的功能是用當(dāng)前的填充顏色或樣式填充路徑線段包圍的區(qū)域。獲取上下文,圖形上下文是什么意思?CGContextRef context = UIGraphicsGetCurrentContext();2、簡單的示例代碼[cpp] view plaincopy// 畫一個(gè)正方形圖形 沒有邊框 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();

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • --繪圖與濾鏡全面解析 概述 在iOS中可以很容易的開發(fā)出絢麗的界面效果,一方面得益于成功系統(tǒng)的設(shè)計(jì),另一方面得益...
    韓七夏閱讀 2,981評論 2 10
  • 原文出處 http://blog.csdn.net/u014286994/article/details/5133...
    Poison_19ce閱讀 1,573評論 0 2
  • 不要在這樣想,你被別人使用還習(xí)慣了,思維不要受限制,能幫就幫,沒必要覺得虧!需要這個(gè)東西很強(qiáng)大,它就像武器一樣 你...
    三不主義閱讀 185評論 0 0
  • “總是向你索取,卻不曾說謝謝你,直到長大以后,才懂得你不容易”。 筷子兄弟的那首《父親》這樣唱到,以前年幼時(shí)聽這首...
    徯我后閱讀 428評論 0 2
  • 曾經(jīng)盡力愛過卻沒有結(jié)果。 初二時(shí),喜歡上了班級的一個(gè)男孩,那時(shí)候的我才朦朦懂什么是喜歡不理解什么是愛!可是班里其他...
    經(jīng)久不息990830閱讀 359評論 0 0

友情鏈接更多精彩內(nèi)容