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í)例圖層屬性來繪制和渲染的必峰,那就是CALayer洪唐。

CALayer類的概念與UIView非常類似,它也具有樹形的層級(jí)關(guān)系吼蚁,并且可以包含圖片文本凭需、背景色等。它與UIView最大的不同在于它不能響應(yīng)用戶交互桂敛,可以說它根本就不知道響應(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圖層才是真正用來在屏幕上顯示的嫁怀,UIView僅僅是對(duì)它的一層封裝,實(shí)現(xiàn)了CALayer的delegate借浊,提供了處理事件交互的具體功能塘淑,還有動(dòng)畫底層方法的高級(jí)API÷旖铮可以說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:圖形上下文,可以通過UIGraphicsGetCurrentContext:獲取當(dāng)前視圖的上下文

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

//圖片合并

-(UIImage*)createLabel{

? ? //開啟圖形上下文

? ? 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)框架,無法直接使用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í)看起來就是一個(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)系作者
  • 序言:七十年代末臂港,一起剝皮案震驚了整個(gè)濱河市森枪,隨后出現(xiàn)的幾起案子视搏,更是在濱河造成了極大的恐慌,老刑警劉巖县袱,帶你破解...
    沈念sama閱讀 221,548評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件浑娜,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡式散,警方通過查閱死者的電腦和手機(jī)棚愤,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來杂数,“玉大人,你說我怎么就攤上這事瘸洛∽嵋疲” “怎么了?”我有些...
    開封第一講書人閱讀 167,990評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵反肋,是天一觀的道長(zhǎng)那伐。 經(jīng)常有香客問我,道長(zhǎng)石蔗,這世上最難降的妖魔是什么罕邀? 我笑而不...
    開封第一講書人閱讀 59,618評(píng)論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮养距,結(jié)果婚禮上诉探,老公的妹妹穿的比我還像新娘。我一直安慰自己棍厌,他們只是感情好肾胯,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,618評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著耘纱,像睡著了一般敬肚。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上束析,一...
    開封第一講書人閱讀 52,246評(píng)論 1 308
  • 那天艳馒,我揣著相機(jī)與錄音,去河邊找鬼员寇。 笑死弄慰,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的丁恭。 我是一名探鬼主播曹动,決...
    沈念sama閱讀 40,819評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼牲览!你這毒婦竟也來了墓陈?” 一聲冷哼從身側(cè)響起恶守,我...
    開封第一講書人閱讀 39,725評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎贡必,沒想到半個(gè)月后兔港,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,268評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡仔拟,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,356評(píng)論 3 340
  • 正文 我和宋清朗相戀三年衫樊,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片利花。...
    茶點(diǎn)故事閱讀 40,488評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡科侈,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出炒事,到底是詐尸還是另有隱情臀栈,我是刑警寧澤,帶...
    沈念sama閱讀 36,181評(píng)論 5 350
  • 正文 年R本政府宣布挠乳,位于F島的核電站权薯,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏睡扬。R本人自食惡果不足惜盟蚣,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,862評(píng)論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望卖怜。 院中可真熱鬧屎开,春花似錦、人聲如沸韧涨。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,331評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽虑粥。三九已至如孝,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間娩贷,已是汗流浹背第晰。 一陣腳步聲響...
    開封第一講書人閱讀 33,445評(píng)論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留彬祖,地道東北人茁瘦。 一個(gè)月前我還...
    沈念sama閱讀 48,897評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像储笑,于是被迫代替她去往敵國和親甜熔。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,500評(píng)論 2 359