iOS 中繪圖的幾種方法

參考文章:https://bihongbo.com/2016/01/03/memoryGhostdrawRect/#more

? ? ? ? ? ? ? ? ? ? http://www.cocoachina.com/ios/20170809/20187.html

? ??????????????????https://zsisme.gitbooks.io/ios-/content/chapter2/custom-drawing.html

iOS提供了兩套繪圖框架,分別是UIBezierPath和Core Graphics。UIBezierPath屬于UIKit。UIBezierPath是對(duì)Core Graphics框架的進(jìn)一步封裝。

OpenGL和Core Graphics都是繪圖專用的API類族,調(diào)用圖形處理器(GPU)進(jìn)行圖形的繪制和渲染。在架構(gòu)上是平級(jí)的,相比UIkit更接近底層。

CALayer 和 UIView區(qū)別

實(shí)際上你所看到的視圖內(nèi)容,包括圖形等,都是由UIView的一個(gè)實(shí)例圖層屬性來(lái)繪制和渲染的,那就是CALayer。

CALayer類的概念與UIView非常類似,它也具有樹(shù)形的層級(jí)關(guān)系,并且可以包含圖片文本、背景色等。它與UIView最大的不同在于它不能響應(yīng)用戶交互,可以說(shuō)它根本就不知道響應(yīng)鏈的存在,它的API雖然提供了“某點(diǎn)是否在圖層范圍內(nèi)的方法”,但是它并不具有響應(yīng)的能力。

在每一個(gè)UIView實(shí)例當(dāng)中,都有一個(gè)默認(rèn)的支持圖層,UIView負(fù)責(zé)創(chuàng)建并且管理這個(gè)圖層。實(shí)際上這個(gè)CALayer圖層才是真正用來(lái)在屏幕上顯示的,UIView僅僅是對(duì)它的一層封裝,實(shí)現(xiàn)了CALayer的delegate,提供了處理事件交互的具體功能,還有動(dòng)畫(huà)底層方法的高級(jí)API??梢哉f(shuō)CALayer是UIView的內(nèi)部實(shí)現(xiàn)細(xì)節(jié)。


1.CAShapeLayer +UIBezierPath

-(void)drawMyLayer{

? ? CGSize size=[UIScreen mainScreen].bounds.size;

? ? CGFloatWIDTH =20.0;

? ? CALayer*layer;

? ? CAShapeLayer* brownRectLayer = [CAShapeLayer layer];

? ? brownRectLayer.frame=CGRectMake(0,0,100,100);

? ? UIBezierPath * path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0,100, 100)];

? ? brownRectLayer.path= path.CGPath;

? ? brownRectLayer.fillColor= [UIColorbrownColor].CGColor;

? ? [self.view.layeraddSublayer:brownRectLayer];

}

效果:

UIGraphicsBeginImageContextWithOptions使用圖片上下文

context:圖形上下文,可以通過(guò)UIGraphicsGetCurrentContext:獲取當(dāng)前視圖的上下文

imageContext:圖片上下文,可以通過(guò)UIGraphicsBeginImageContextWithOptions:獲取一個(gè)圖片上下文,然后繪制完成后,調(diào)用UIGraphicsGetImageFromCurrentImageContext獲取繪制的圖片,最后要記得關(guān)閉圖片上下文UIGraphicsEndImageContext。

//圖片合并

-(UIImage*)createLabel{

? ? //開(kāi)啟圖形上下文

? ? NSString*text =@"123456";

? ? UIImage*image = [UIImageimageNamed:@"icon_60pt"];

? ? UIGraphicsBeginImageContextWithOptions(image.size, NO,1.0);

? ? //將圖片繪制到

? ? [imagedrawInRect:CGRectMake(0,0,image.size.width, image.size.height)];

? ? NSDictionary*attr =@{

?? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFontboldSystemFontOfSize:20],? //設(shè)置字體 ? ? ? ? ? ? ? ? ? ? ? ? ? ????????NSForegroundColorAttributeName: [UIColorredColor]? ? ? //設(shè)置字體顏色 ? ? ? ? ? ? ? ? ? ? ??

? ? };

? ? [textdrawAtPoint:CGPointMake(20, 20) withAttributes:attr];

? ? UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

? ? UIGraphicsEndImageContext();

? ? returnnewImage;

}


單純使用CaLayer

?//獲得根圖層

? ? layer=[[CALayeralloc]init];

? ? //設(shè)置背景顏色,由于QuartzCore是跨平臺(tái)框架,無(wú)法直接使用UIColor

? ? layer.backgroundColor=[UIColor colorWithRed:0 green:146/255.0 blue:1.0 alpha:1.0].CGColor;

? ? //設(shè)置中心點(diǎn)

? ? layer.position=CGPointMake(size.width/2, size.height/2);

? ? //設(shè)置大小

? ? layer.bounds=CGRectMake(0,0, WIDTH,WIDTH);

? ? //設(shè)置圓角,當(dāng)圓角半徑等于矩形的一半時(shí)看起來(lái)就是一個(gè)圓形

? ? layer.cornerRadius=WIDTH/2;

? ? //設(shè)置陰影

? ? layer.shadowColor=[UIColor grayColor].CGColor;

? ? layer.shadowOffset=CGSizeMake(2,2);

? ? layer.shadowOpacity=.9;

? ? //設(shè)置邊框

? ? //? ? layer.borderColor=[UIColor whiteColor].CGColor;

? ? //? ? layer.borderWidth=1;

? ? //設(shè)置錨點(diǎn)

? ? //? ? layer.anchorPoint=CGPointZero;

? ? [self.view.layeraddSublayer:layer];



CGContextRef

- (void)drawRect:(CGRect)rect {

? ? //獲取圖形上下文


? ? CGContextRef ctx = UIGraphicsGetCurrentContext();

? ? CGContextSetStrokeColorWithColor(ctx,[UIColor redColor].CGColor);

? ? CGContextSetLineWidth(ctx,2.0);

? ? CGContextMoveToPoint(ctx,80,30);

? ? CGContextAddLineToPoint(ctx,80,150);

? ? CGContextStrokePath(ctx);


? ? CGContextSetStrokeColorWithColor(ctx, [UIColor blueColor].CGColor);

? ? CGContextMoveToPoint(ctx,100,30);

? ? CGContextAddLineToPoint(ctx,100,100);

? ? CGContextStrokePath(ctx);


? ? CGContextMoveToPoint(ctx,120,30);

? ? CGContextAddLineToPoint(ctx,120,100);

? ? CGContextStrokePath(ctx);


? ? CGContextMoveToPoint(ctx,150,30);

? ? CGContextAddLineToPoint(ctx,190,30);

? ? CGContextStrokePath(ctx);


? ? //CGContextAddRect(ctx, CGRectMake(0,0,100,100));


? ? CGContextFillRect(ctx, CGRectMake(0,0,1000,1000));

? ? CGContextSetStrokeColorWithColor(ctx,[UIColor redColor].CGColor);;

? ? CGContextStrokePath(ctx);


? ? /*

? ? [[UIColor orangeColor] set];

? ? UIBezierPath *path = [UIBezierPath bezierPath];

? ? path.lineWidth = 2.0;

? ? [path moveToPoint:CGPointMake(80,100)];

? ? [path? addLineToPoint:CGPointMake(80,150)];

? ? [path stroke];


? ? UIBezierPath *cirlePaht = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(80-10,150,20,20)];

? ? [[UIColor grayColor]set];

? ? cirlePaht.lineWidth = 5/3;

? ? [cirlePaht stroke];

? ? [cirlePaht fill];


? ? UIBezierPath *paths = [UIBezierPath bezierPath];

? ? paths.lineWidth = 2.0;

? ? [paths moveToPoint:CGPointMake(80,170)];

? ? [paths? addLineToPoint:CGPointMake(80,200)];

? ? [paths stroke];

? ? */

}


單純UIBezierPath

- (void)drawRect:(CGRect)rect {

? ? //獲取圖形上下文

? ? /*

? ? CGContextRef ctx = UIGraphicsGetCurrentContext();

? ? CGContextSetStrokeColorWithColor(ctx,[UIColor redColor].CGColor);

? ? CGContextSetLineWidth(ctx,2.0);

? ? CGContextMoveToPoint(ctx,80,30);

? ? CGContextAddLineToPoint(ctx,80,150);

? ? CGContextStrokePath(ctx);


? ? CGContextSetStrokeColorWithColor(ctx, [UIColor blueColor].CGColor);

? ? CGContextMoveToPoint(ctx,100,30);

? ? CGContextAddLineToPoint(ctx,100,100);

? ? CGContextStrokePath(ctx);


? ? CGContextMoveToPoint(ctx,120,30);

? ? CGContextAddLineToPoint(ctx,120,100);

? ? CGContextStrokePath(ctx);


? ? CGContextMoveToPoint(ctx,150,30);

? ? CGContextAddLineToPoint(ctx,190,30);

? ? CGContextStrokePath(ctx);


? ? //CGContextAddRect(ctx, CGRectMake(0,0,100,100));


? ? CGContextFillRect(ctx, CGRectMake(0,0,100,100));

? ? CGContextSetStrokeColorWithColor(ctx,[UIColor redColor].CGColor);;

? ? CGContextStrokePath(ctx);

? ? */


? ? [[UIColor orangeColor] set];

? ? UIBezierPath *path = [UIBezierPath bezierPath];

? ? path.lineWidth=2.0;

? ? [pathmoveToPoint:CGPointMake(80,100)];

? ? [path? addLineToPoint:CGPointMake(80,150)];

? ? [pathstroke];


? ? UIBezierPath *cirlePaht = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(80-10,150,20,20)];

? ? [[UIColor grayColor]set];

? ? cirlePaht.lineWidth=5/3;

? ? [cirlePahtstroke];

? ? [cirlePahtfill];


? ? UIBezierPath *paths = [UIBezierPath bezierPath];

? ? paths.lineWidth=2.0;

? ? [pathsmoveToPoint:CGPointMake(80,170)];

? ? [paths? addLineToPoint:CGPointMake(80,200)];

? ? [pathsstroke];

}


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容