Quartz2D總結,iosquartz2d
天了嚕,腦子完全懵了,最起碼說出來個上下文啊,連這個都給忘了,特此總結一下,並以此緬懷這次面試
Quartz2D的API來自於Core Graphics(這就是為什麼CGContextRef是以CG開頭),需要匯入CoreGraphics(?)
圖形上下文:是一個CGContextRef類型的資料
圖形內容相關的作用:
1.儲存繪圖資訊、繪圖狀態
2.決定繪製的輸出目標(繪製到什麼地方去)(輸出目標可以是PDF檔案、Bitmap或者顯示器的視窗上
Bitmap Graphics ContextPDF Graphics ContextWindow Graphics ContextLayer Graphics ContextPrinter Graphics Context
自己畫一個view
步驟:
1.建立一個類,繼承自UIView
2.實現- (void)drawRect:(CGRect)rect方法,並在此方法中取得當前view的圖形上下文
3.建立自己的繪製path,並根據path來繪製圖形
4.利用圖形上下文將繪製的所有內容渲染顯示到view上面
當然,比如Bitmap的圖形上下文就不必在- (void)drawRect:(CGRect)rect裡面了,直接建立一個就可:
// 建立一個bitmap上下文 UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 200), NO, 0); // 擷取bitmap上下文 CGContextRef ctr = UIGraphicsGetCurrentContext(); // 畫圓 CGContextAddEllipseInRect(ctr, CGRectMake(50, 50, 100, 100)); // 渲染 CGContextStrokePath(ctr); // 從上下文擷取畫出的圖片 UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); UIImageView *imageView = [[UIImageView alloc] initWithImage:img]; CGRect frame = imageView.frame; frame.origin = CGPointMake(50, 50); imageView.frame = frame; [self.view addSubview:imageView];
待續。。。
部分參考自:http://www.jianshu.com/p/eecffec3b7af