iOS學習筆記08-Quartz2D繪圖

來源:互聯網
上載者:User

標籤:上下文   dict   ota   ntc   模式   atp   繪製映像   was   sel   

一、Quartz2D簡單介紹

在iOS中常用的繪圖架構就是Quartz2D,Quartz2D是Core Graphics架構的一部分,我們日常開發使用的所有UIKit組件都是由Core Graphics進行繪製的

在iOS中Quartz2D繪圖的一般步驟:
  1. 擷取繪製上下文
  2. 建立並設定路徑
  3. 將路徑添加進繪製上下文中
  4. 設定上下文狀態
  5. 繪製路徑
  6. 釋放路徑

UIKit預設為我們提供了一個圖形上下文,在UI控制項的drawRect:方法中調用UIGraphicsGetCurrentContext()擷取圖形上下文

下面是使用執行個體,在自訂UIView類內定義:
#pragma mark 繪圖   - (void)drawRect:(CGRect)rect{ //繪圖只能在此方法中調用,否則無法得到當前圖形上下文    //1.取得圖形內容物件 CGContextRef context = UIGraphicsGetCurrentContext(); //2.建立路徑對象 CGMutablePathRef path = CGPathCreateMutable(); CGPathMoveToPoint(path, nil, 20, 50);//移動到指定位置(設定路徑起點) CGPathAddLineToPoint(path, nil, 20, 100);//繪製直線(從起始位置開始) CGPathAddLineToPoint(path, nil, 300, 100);//繪製另外一條直線(從上一直線終點開始繪製) //3.添加路徑到圖形上下文 CGContextAddPath(context, path); //4.設定圖形上下文狀態屬性 CGContextSetRGBStrokeColor(context, 1.0, 0, 0, 1);//設定筆觸顏色 CGContextSetRGBFillColor(context, 0, 1.0, 0, 1);//設定填充色 CGContextSetLineWidth(context, 2.0);//設定線條寬度 CGContextSetLineCap(context, kCGLineCapRound);//設定頂點樣式 CGContextSetLineJoin(context, kCGLineJoinRound);//設定連接點樣式 /*設定線段樣式 CGContextSetLineDash(context, phase, lengths, count); phase:虛線開始的位置 lengths:虛線長度間隔 count:虛線數組元素個數 */ CGFloat lengths[2] = { 18, 9 }; CGContextSetLineDash(context, 0, lengths, 2); /*設定陰影 CGContextSetShadowWithColor(context, offset, blur, color); offset:位移量 blur:模糊度 color:陰影顏色 */ CGColorRef color = [UIColor grayColor].CGColor;//Quartz2D是跨平台的,使用的是C語言 CGContextSetShadowWithColor(context, CGSizeMake(2, 2), 0.8, color); //5.繪製映像到指定圖形上下文 /*填充方式CGPathDrawingMode kCGPathFill:只有填充(非零纏繞數填充),不繪製邊框 kCGPathEOFill:奇偶規則填充(多條路徑交叉時,奇數交叉填充,偶交叉不填充) kCGPathStroke:只有邊框 kCGPathFillStroke:既有邊框又有填充 kCGPathEOFillStroke:奇偶填充並繪製邊框 */ CGContextDrawPath(context, kCGPathFillStroke);//最後一個參數是填滿類型 //6.釋放路徑對象 CGPathRelease(path);}

上面的繪製過程太過於麻煩,所以蘋果為我們封裝了一些繪製方法在UIKit架構中

- (void)drawRect:(CGRect)rect{        //1.獲得圖形上下文        CGContextRef context = UIGraphicsGetCurrentContext(); //2.添加路徑(相當於前面建立路徑並添加路徑到圖形上下文兩步操作) CGContextMoveToPoint(context, 20, 50); CGContextAddLineToPoint(context, 20, 100); CGContextAddLineToPoint(context, 300, 100); //封閉路徑:直接調用路徑封閉方法 CGContextClosePath(context); //3.設定圖形內容屬性 [[UIColor redColor] setStroke];//設定紅色邊框 [[UIColor greenColor] setFill];//設定綠色填充 //[[UIColor blueColor] set];//同時設定填充和邊框色 //4.繪製路徑 CGContextDrawPath(context, kCGPathFillStroke);}
二、基本圖形的繪圖方法
# 繪製矩形CGContextAddRect(CGContextRef context, CGRect rect);UIRectFill(CGRect rect);//只有填充UIRectFrame(CGRect rect);//只有邊框# 繪製橢圓CGContextAddEllipseInRect(CGContextRef context, CGRect rect);# 繪製弧度/* context:繪製上下文 x:中心點x座標 y:中心點y座標 radius:半徑 startAngle:起始弧度,0表示圓的最右邊開始 endAngle:終止弧度,正值表示順時針計算 closewise:是否逆時針繪製,0則順時針繪製*/CGContextAddArc(CGContextRef context, CGFloat x, CGFloat y, CGFloat radius, CGFloat startAngle, CGFloat endAngle, int closewise);# 繪製二次方貝茲曲線/* context:圖形上下文 cpx:控制點x座標 cpy:控制點y座標 x:結束點x座標 y:結束點y座標 */CGContextAddQuadCurveToPoint(CGContextRef context, CGFloat cpx, CGFloat cpy, CGFloat x, CGFloat y);# 繪製三次貝茲路徑/* context:圖形上下文 cp1x:第一個控制點x座標 cp1y:第一個控制點y座標 cp2x:第二個控制點x座標 cp2y:第二個控制點y座標 x:結束點x座標 y:結束點y座標 */CGContextAddCurveToPoint(CGContextRef context, CGFloat cp1x, CGFloat cp1y, CGFloat cp2x, CGFloat cp2y, CGFloat x, CGFloat y);

三、繪製文本和映像
#pragma mark 繪圖- (void)drawRect:(CGRect)rect{    //1.獲得圖形上下文        CGContextRef context = UIGraphicsGetCurrentContext(); [self drawText:context]; [self drawImage:context];}#pragma mark 繪製文本- (void)drawText:(CGContextRef)context{ //繪製到指定的地區內容 NSString *str = @"Star Walk is the most beautiful ......sdadasd dsadsa"; CGRect rect = CGRectMake(20, 50, 280, 300); UIFont *font = [UIFont systemFontOfSize:18];//設定字型 UIColor *color = [UIColor redColor];//字型顏色 NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];//段落樣式 style.alignment = NSTextAlignmentLeft;//對齊 [str drawInRect:rect withAttributes:@{NSFontAttributeName:font, NSForegroundColorAttributeName:color, NSParagraphStyleAttributeName:style}];}#pragma mark 繪製圖片- (void)drawImage:(CGContextRef)context{ UIImage *image = [UIImage imageNamed:@"image2.jpg"]; //從某一點開始繪製 [image drawAtPoint:CGPointMake(10, 50)]; //繪製到指定的矩形中,注意如果大小不合適會會進行展開,映像會形變 [image drawInRect:CGRectMake(10, 50, 300, 450)]; //平鋪繪製 [image drawAsPatternInRect:CGRectMake(0, 0, 320, 568)];}

實際上還有一些繪圖方法,比如漸層填充、疊加模式和填充模式,但這些東西使用的不多,這裡就不細講了,有興趣我可能會另外寫個筆記針對這些繪圖技巧。
可以參考下面效果:

四、圖形上下文形變

Quartz2D的座標系統UIKit並不一樣,它的座標原點在螢幕左下角,UIKit對其進行了轉換,座標原點統一在螢幕左上方

我們也可以對座標系進行操作:
#pragma mark 圖形上下文形變-(void)drawRect:(CGRect rect){    CGContextRef context = UIGraphicsGetCurrentContext(); //儲存初始狀態 CGContextSaveGState(context); //形變第一步:圖形上下文向右平移40 CGContextTranslateCTM(context, 100, 0); //形變第二步:縮放0.8 CGContextScaleCTM(context, 0.8, 0.8); //形變第三步:旋轉 CGContextRotateCTM(context, M_PI_4/4); UIImage *image = [UIImage imageNamed:@"photo1.jpg"]; [image drawInRect:CGRectMake(0, 50, 240, 300)]; //恢複到初始狀態 CGContextRestoreGState(context);}
  • 設定圖形上下文形變之前一定要注意儲存內容相關的初始狀態,在使用完之後進行恢複
  • 在iOS開發中不允許開發人員直接調用drawRect:方法,重新整理繪製內容需要調用setNeedsDisplay方法。
五、其他繪製上下文位元影像上下文:
# 開啟位元影像上下文,沒有傳回值UIGraphicsBeginImageContext(CGSize size);# 在開啟位元影像上下文和關閉位元影像上下文之間,使用該方法擷取位元影像上下文UIGraphicsGetCurrentContext()# 關閉位元影像上下文,沒有傳回值UIGraphicsEndImageContext()
PDF上下文:
# 開啟PDF上下文,沒有傳回值/*     path : 儲存路徑     bounds : pdf文檔大小,如果設定為CGRectZero,則使用預設值:612*792     pageInfo : 版面設定,為nil則不設定任何資訊*/UIGraphicsBeginPDFContextToFile(NSString *path, CGRect bounds, NSDictionary *pageInfo);# 在開啟PDF上下文和關閉PDF上下文之間,使用該方法擷取PDF上下文UIGraphicsGetCurrentContext()# 關閉PDF上下文,沒有傳回值UIGraphicsEndPDFContext()

我們怎麼知道我們繪製成功了呢?

對於PDF,我們可以開啟看PDF檔案對於位元影像,我們可以把繪製後的位元影像儲存為圖片對象UIImage
# 返回繪製的新映像UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
一個PDF繪製的小技巧,就是PDF的分頁
# 開啟一個PDF新頁,調用了該方法,以後的繪圖操作都是在新頁上繪製的IGraphicsBeginPDFPage()

iOS學習筆記08-Quartz2D繪圖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.