国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

iOS繪圖

瀏覽:36日期:2022-09-17 13:42:08
介紹

說(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

兩大繪圖框架: UIKit 像UIImage、NSString(繪制文本)、UIBezierPath(繪制形狀)、UIColor都知道如何繪制自己。 這些類(lèi)提供了功能有限但使用方便的方法來(lái)讓我們完成繪圖任務(wù)。一般情況下,UIKit就是我們所需要的。 Core Graphics 這是一個(gè)繪圖專(zhuān)用的API族,它經(jīng)常被稱(chēng)為QuartZ或QuartZ 2D。Core Graphics是iOS上所有繪圖 功能的基石,包括UIKit。6種繪圖的形式

第一種UIKit框架drawRect:

在UIView的子類(lèi)方法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)題比如當(dāng)在一個(gè)view上繪制一條線(xiàn)之類(lèi)的,是直接用addsubView添加一個(gè)UIView控件好,還是在drawRect:里用繪圖代碼繪制一條線(xiàn)好?哪種更高效,或者一樣?源碼,應(yīng)用場(chǎng)景

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

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

標(biāo)簽: IOS
相關(guān)文章:
主站蜘蛛池模板: 国产成人精品亚洲 | 亚洲一区欧美一区 | 国产精品国产精品国产三级普 | 国产成人一区二区三区免费观看 | 成人污网站| 亚洲国产欧美视频 | 日韩一区二区三区在线视频 | 日本加勒比系列 | 国产99视频精品免视看7 | 亚洲美女中文字幕 | 午夜免费理论片a级 | 亚洲视频在线免费看 | 欧美久色| 黄色美女免费看 | 视频综合网| 国产伦码精品一区二区三区 | 欧美精品在线视频 | 6080伦理久久精品亚洲 | 手机看片1024久久精品你懂的 | 三级网站大全 | 男女男精品视频网站 | 国产视频久 | 免费国产成人18在线观看 | 加勒比在线免费视频 | 久久精品网站免费观看 | 国产美女一区精品福利视频 | 亚洲综合网在线 | 97国产在线播放 | 精品中文字幕久久久久久 | 日韩毛片欧美一级国产毛片 | 欧美顶级毛片在线播放 | 国产成人久久综合热 | 国产免费一区二区三区在线 | 成人夜色香网站在线观看 | 欧美综合自拍亚洲综合 | 亚洲www.| 国产伦精品一区二区三区网站 | 亚洲精品一区二区三区中文字幕 | 一级全黄毛片 | 在线观看黄网视频免费播放 | 国产成人精品区在线观看 |