中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

iOS繪圖

2018-07-20    來(lái)源:編程學(xué)習(xí)網(wǎng)

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用

介紹

說(shuō)到iOS的繪圖肯定就是Core Graphics。

Core Graphics Framework是一套基于C的API框架,使用了Quartz作為繪圖引擎。它提供了低級(jí)別、輕量級(jí)、高保真度的2D渲染。該框架可以用于基于路徑的繪圖、變換、顏色管理、脫屏渲染,模板、漸變、遮蔽、圖像數(shù)據(jù)管理、圖像的創(chuàng)建、遮罩以及PDF文檔的創(chuàng)建、顯示和分析。

獲取圖形上下文

Core Graphics API所有的操作都在一個(gè)上下文中進(jìn)行。所以在繪圖之前需要獲取該上下文并傳入執(zhí)行渲染的函數(shù)中。如果你正在渲染一副在內(nèi)存中的圖片,此時(shí)就需要傳入圖片所屬的上下文。獲得一個(gè)圖形上下文是我們完成繪圖任務(wù)的第一步,你可以將圖形上下文理解為一塊畫(huà)布。如果你沒(méi)有得到這塊畫(huà)布,那么你就無(wú)法完成任何繪圖操作。

獲取圖形上下文的幾種方式: 1.drawRect: 2.inContext:    (-(void)drawInContext:(CGContextRef)ctx   - (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)ctx) 3.UIGraphicsBeginImageContextWithOptions

兩大繪圖框架: UIKitUIImageNSString(繪制文本)、UIBezierPath(繪制形狀)、UIColor都知道如何繪制自己。
 這些類提供了功能有限但使用方便的方法來(lái)讓我們完成繪圖任務(wù)。一般情況下,UIKit就是我們所需要的。

 Core Graphics
 這是一個(gè)繪圖專用的API族,它經(jīng)常被稱為QuartZ或QuartZ 2D。Core Graphics是iOS上所有繪圖
 功能的基石,包括UIKit。

6種繪圖的形式

  • 第一種UIKit框架drawRect:

    UIView的子類方法drawRect:中繪制一個(gè)藍(lán)色圓
    - (void) drawRect: (CGRect) rect { UIBezierPath* p = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0,0,100,100)]; 
        [[UIColor blueColor] setFill]; 
        [p fill]; 
    }
  • 第二種Core Graphics框架inContext:

    - (void)drawRect:(CGRect)rect{ //當(dāng)前上下文及畫(huà)布為當(dāng)前view CGContextRef con = UIGraphicsGetCurrentContext(); CGContextAddEllipseInRect(con, CGRectMake(0,0,100,100)); CGContextSetFillColorWithColor(con, [UIColor blueColor].CGColor); CGContextFillPath(con);
    }

  • 第三種UIKit框架inContext:

    drawInContext:方法 @interface TestLayer : CALayer @end @implementation TestLayer - (void)drawInContext:(CGContextRef)ctx{ UIGraphicsPushContext(ctx); UIBezierPath* p = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0,0,100,100)];
          [[UIColor blueColor] setFill];
          [p fill]; UIGraphicsPopContext();
    } @end @implementation XXXViewController{
    - (void)viewDidLoad{
    
          [super viewDidLoad]; //1.創(chuàng)建自定義的layer TestLayer *layer=[TestLayer layer]; //2.設(shè)置layer的屬性 layer.backgroundColor= [UIColor blackColor].CGColor;
          layer.frame=CGRectMake(100, 100, 200, 200);
    
          [layer setNeedsDisplay]; //3.添加layer [self.view.layer addSublayer:layer];
    } @end
drawLayer: inContext:方法 @interface MyLayerDelegate : NSObject @end @implementation MyLayerDelegate - (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)ctx { UIGraphicsPushContext(ctx); UIBezierPath* p = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0,0,100,100)];
        [[UIColor blueColor] setFill];
        [p fill]; UIGraphicsPopContext();
  } @end @implementation XXXViewController{

        MyLayerDelegate *_layerDeleagete; CALayer *_layer;
  }

  - (void)viewDidLoad{

        [super viewDidLoad];
        _layerDeleagete = [[MyLayerDelegate alloc] init]; //1.創(chuàng)建自定義的layer _layer=[CALayer layer]; //2.設(shè)置layer的屬性 _layer.backgroundColor= [UIColor blackColor].CGColor;
        _layer.frame=CGRectMake(100, 100, 200, 200);

        _layer.delegate = _layerDeleagete;
        [_layer setNeedsDisplay]; //3.添加layer [self.view.layer addSublayer:_layer];
  }

  - (void)dealloc{
        _layer.delegate = nil;
  } @end
  • 第四種Core Graphics框架inContext:

    drawInContext:方法
    - (void)drawInContext:(CGContextRef)ctx{ CGContextAddEllipseInRect(ctx, CGRectMake(0,0,100,100)); CGContextSetFillColorWithColor(ctx, [UIColor blueColor].CGColor); CGContextFillPath(ctx);
    }
drawLayer: inContext:方法
  - (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)ctx { UIGraphicsPushContext(ctx); UIBezierPath* p = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0,0,100,100)];
        [[UIColor blueColor] setFill]; [p fill]; UIGraphicsPopContext(); 
  }

  • 第五種UIKit框架UIGraphicsBeginImageContextWithOptions

    @implementation XXXViewController - (void)viewDidLoad{
    
        [super viewDidLoad]; UIGraphicsBeginImageContextWithOptions(CGSizeMake(100,100), NO, 0); UIBezierPath* p = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0,0,100,100)];
        [[UIColor blueColor] setFill];
        [p fill]; UIImage* im = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); /*---------------------------------*/ UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
        [imageView setImage:im];
        [self.view addSubview:imageView];
    } @end
  • 第六種Core Graphics框架UIGraphicsBeginImageContextWithOptions

    @implementation XXXViewController - (void)viewDidLoad{
    
        [super viewDidLoad]; UIGraphicsBeginImageContextWithOptions(CGSizeMake(100,100), NO, 0); CGContextRef con = UIGraphicsGetCurrentContext(); CGContextAddEllipseInRect(con, CGRectMake(0,0,100,100)); CGContextSetFillColorWithColor(con, [UIColor blueColor].CGColor); CGContextFillPath(con); UIImage* im = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); /*---------------------------------*/ UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
        [imageView setImage:im];
        [self.view addSubview:imageView];
    }

注意

  • 使用- (void)drawRect:(CGRect)rect需要注意的地方

    以下方法調(diào)用drawRect

    1.如果在UIView初始化時(shí)沒(méi)有設(shè)置rect大小,將直接導(dǎo)致drawRect不被自動(dòng)調(diào)用。

    2.該方法在調(diào)用sizeThatFits后被調(diào)用,所以可以先調(diào)用sizeToFit計(jì)算出size。然后系統(tǒng)自動(dòng)調(diào)用drawRect:方法。

    3.通過(guò)設(shè)置contentMode屬性值為UIViewContentModeRedraw。那么將在每次設(shè)置或更改frame的時(shí)候自動(dòng)調(diào)用drawRect:。

    4.直接調(diào)用setNeedsDisplay,或者setNeedsDisplayInRect:觸發(fā)drawRect:,但是有個(gè)前提條件是rect不能為0.

  • 若要實(shí)時(shí)畫(huà)圖,不能使用gestureRecognizer,只能使用touchbegan等方法來(lái)掉用setNeedsDisplay實(shí)時(shí)刷新屏幕

問(wèn)題

  1. 比如當(dāng)在一個(gè)view上繪制一條線之類的,是直接用addsubView添加一個(gè)UIView控件好,還是在drawRect:里用繪圖代碼繪制一條線好?
    哪種更高效,或者一樣?

源碼,應(yīng)用場(chǎng)景

以上六種方式繪制圓的代碼
繪圖代碼比較常用就是圖表繪畫(huà)板這兩種場(chǎng)景。

兩個(gè)可以學(xué)習(xí)的源碼:
圖表:BEMSimpleLineGraph
繪畫(huà)板:Brushes

標(biāo)簽: isp 代碼

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:關(guān)于App程序員泡沫

下一篇:學(xué)習(xí)編程之路