15.ios之Quartz2D

來源:互聯網
上載者:User

15.ios之Quartz2D

1.什麼是Quartz2D

 

•Quartz2D是一個二維繪圖引擎,同時支援iOS和Mac系統 •Quartz 2D能完成的工作 繪製圖形 : 線條三角形矩形圓弧等 繪製文字 繪製產生圖片(映像) 讀取產生PDF 裁剪圖片 自訂UI控制項

 

2.自訂View

圖形上下文

 

•圖形上下文(Graphics Context):是一個CGContextRef類型的資料
•圖形內容相關的作用 儲存繪圖資訊、繪圖狀態 決定繪製的輸出目標(繪製到什麼地方去?)

 

(輸出目標可以是PDF檔案、Bitmap或者顯示器的視窗上)



drawRect:中取得的上下文

•在drawRect:方法中取得上下文後,就可以繪製東西到view上
•View內部有個layer(圖層)屬性,drawRect:方法中取得的是一個Layer Graphics Context,因此,繪製的東西其實是繪製到view的layer上去了
•View之所以能顯示東西,完全是因為它內部的layer

Quartz2D繪圖的代碼步驟
獲得圖形上下文CGContextRef ctx = UIGraphicsGetCurrentContext();拼接路徑(下面代碼是搞一條線段)CGContextMoveToPoint(ctx, 10, 10);CGContextAddLineToPoint(ctx, 100, 100);繪製路徑CGContextStrokePath(ctx); // CGContextFillPath(ctx);


常用拼接路徑函數

建立一個起點void CGContextMoveToPoint(CGContextRef c, CGFloat x, CGFloat y)添加新的線段到某個點void CGContextAddLineToPoint(CGContextRef c, CGFloat x, CGFloat y)添加一個矩形void CGContextAddRect(CGContextRef c, CGRect rect)添加一個橢圓void CGContextAddEllipseInRect(CGContextRef context, CGRect rect)添加一個圓弧void CGContextAddArc(CGContextRef c, CGFloat x, CGFloat y,  CGFloat radius, CGFloat startAngle, CGFloat endAngle, int clockwise)


常用繪製路徑函數

Mode參數決定繪製的模式void CGContextDrawPath(CGContextRef c, CGPathDrawingMode mode)繪製空心路徑void CGContextStrokePath(CGContextRef c)繪製實心路徑void CGContextFillPath(CGContextRef c)提示:一般以CGContextDraw、CGContextStroke、CGContextFill開頭的函數,都是用來繪製路徑的


圖形上下文棧的操作

將當前的上下文copy一份,儲存到棧頂(那個棧叫做”圖形上下文棧”)void CGContextSaveGState(CGContextRef c)將棧頂的上下文出棧,替換掉當前的上下文void CGContextRestoreGState(CGContextRef c)


矩陣操作

利用矩陣操作,能讓繪製到上下文中的所有路徑一起發生變化縮放void CGContextScaleCTM(CGContextRef c, CGFloat sx, CGFloat sy)旋轉void CGContextRotateCTM(CGContextRef c, CGFloat angle)平移void CGContextTranslateCTM(CGContextRef c, CGFloat tx, CGFloat ty)


圖片浮水印

開啟一個基於位元影像的圖形上下文void     UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale)從上下文中取得圖片(UIImage)UIImage* UIGraphicsGetImageFromCurrentImageContext();結束基於位元影像的圖形上下文void     UIGraphicsEndImageContext();


圖片裁剪

 

void CGContextClip(CGContextRef c)將當前上下所繪製的路徑裁剪出來(超出這個裁剪地區的都不能顯示)

 

 


螢幕

 

- (void)renderInContext:(CGContextRef)ctx;調用某個view的layer的renderInContext:方法即可


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.