長(zhǎng)圖雷则、截圖、繪圖


title: 長(zhǎng)圖懈玻、截圖巧婶、繪圖
date: 2017-11-27
categories:
tags: Objective-C,


關(guān)于圖片繪制的內(nèi)容有很多,很難用一篇文章來介紹涂乌。這篇文章只是在工作中遇到的情況艺栈。根據(jù)不同的情況來分別記錄一下當(dāng)時(shí)是怎么處理的。

[TOC]

長(zhǎng)圖

在某些滾動(dòng)視圖里面湾盒,可能遇到一張長(zhǎng)圖來展示所有的內(nèi)容湿右。
在繼承UIScrollView的視圖里,可以通過如下方式來生成長(zhǎng)圖罚勾。

UIImage* image = nil;
UIGraphicsBeginImageContext(scrollView.contentSize);
{//重點(diǎn)毅人,既要獲取整個(gè)scrollerView,又要保證不破環(huán)界面本身
CGPoint savedContentOffset = scrollView.contentOffset; // 內(nèi)容偏移
CGRect savedFrame = scrollView.frame;
scrollView.contentOffset = CGPointZero;
scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);
[scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();
scrollView.contentOffset = savedContentOffset;
scrollView.frame = savedFrame;
}
UIGraphicsEndImageContext();
使用
UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, scrollView.opaque, 0.0);
替換
UIGraphicsBeginImageContext(scrollView.contentSize);
處理圖片失真的問題。

tlayer層的大小取決于frame尖殃,不是contentSize丈莺。

某個(gè)UI控件的內(nèi)容

這個(gè)其實(shí)是最常見的需求情況,比如某個(gè)圖片是某種宣傳渠道送丰。你們后臺(tái)沒有搞不同機(jī)型返回不同的圖片缔俄。
其實(shí)這個(gè)時(shí)候完全可以直接把讀取到的圖片,但是器躏,有的時(shí)候圖片需要進(jìn)行處理(比如圓角俐载、邊框、顯示卡片的樣式等)登失。這種情況下沒辦法對(duì)網(wǎng)絡(luò)請(qǐng)求的圖片直接存儲(chǔ)遏佣。這時(shí)可以把需要生成圖片的控件做繪制存儲(chǔ)。

UIGraphicsBeginImageContext(currentView.bounds.size); //currentView 當(dāng)前的view  創(chuàng)建一個(gè)基于繪圖的圖形上下文并指定大小為當(dāng)前視圖的bounds
[currentView.layer renderInContext:UIGraphicsGetCurrentContext()]; //renderInContext呈現(xiàn)接受者及其子范圍到指定的上下文
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); //返回一個(gè)基于當(dāng)前圖形上下文的圖片
UIGraphicsEndImageContext(); //移除棧頂?shù)幕诋?dāng)前位圖的圖形上下文
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil); //然后將該圖片保存到圖片圖

在畫布上添加UI元素來繪制圖片

這種需求可能會(huì)在下面這樣的場(chǎng)景中遇到:宣傳用的落地頁(yè)揽浙。
因?yàn)檫@種場(chǎng)景中一般都會(huì)有數(shù)額等信息状婶,這些數(shù)值每個(gè)用戶是不一樣的。這個(gè)時(shí)候就需要在模板上繪制出這些會(huì)變動(dòng)的信息捏萍。
以某個(gè)項(xiàng)目中的戰(zhàn)績(jī)海報(bào)為例:

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

+ (UIImage *)createShareImageWithID:(NSString *)userid totalIncome:(NSString *)totalIncome taskIncome:(NSString *)taskIncome apprenticeIncome:(NSString *)apprenticeIncome apprenticeNumber:(NSString *)apprenticeNumber userIcon:(NSString *)userIcon qrCode:(NSString *)qrCode {
UIImage *image = [UIImage imageNamed: @"imageNameString"];
CGSize size= CGSizeMake(image.size.width, image.size.height); // 畫布大小 圖片大小固定的.
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0); // 尺寸 不透明 無(wú)縮放
[image drawAtPoint:CGPointMake(0, 0)];
// 獲得一個(gè)位圖圖形上下文
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawPath (context, kCGPathStroke);

//繪制二維碼
NSData *ewmData = [NSData dataWithContentsOfURL:[NSURL URLWithString:qrCode]];
UIImage *ewmImage = [UIImage imageWithData:ewmData];
[ewmImage drawInRect:CGRectMake(90, 750, 140, 140) ];

//
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
paragraphStyle.alignment = NSTextAlignmentCenter;

NSMutableAttributedString *apprenticeAttributedStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ 人", apprenticeNumber]];
NSUInteger apprenticeStrLength = apprenticeAttributedStr.length;
[apprenticeAttributedStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:52.f],NSForegroundColorAttributeName:UIColorFromRGB(0x27313e),NSParagraphStyleAttributeName:paragraphStyle} range:NSMakeRange(0, apprenticeStrLength-2)];
[apprenticeAttributedStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:28.f],NSForegroundColorAttributeName:UIColorFromRGB(0x27313e),NSParagraphStyleAttributeName:paragraphStyle} range:NSMakeRange(apprenticeStrLength-1, 1)];
[apprenticeAttributedStr drawInRect:CGRectMake(0, 610, 375, 300)];

NSMutableAttributedString *totalIncomeAttributeStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ 元", apprenticeIncome]];
NSUInteger totalIncomeStrLength = totalIncomeAttributeStr.length;
[totalIncomeAttributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:52.f],NSForegroundColorAttributeName:UIColorFromRGB(0x27313e),NSParagraphStyleAttributeName:paragraphStyle} range:NSMakeRange(0, totalIncomeStrLength-2)];
[totalIncomeAttributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:28.f],NSForegroundColorAttributeName:UIColorFromRGB(0x27313e),NSParagraphStyleAttributeName:paragraphStyle} range:NSMakeRange(totalIncomeStrLength-1, 1)];
[totalIncomeAttributeStr drawInRect:CGRectMake(375, 610, 375, 300)];

UIImage *tempImg = [UIImage imageNamed:@"show_btn"];
[tempImg drawInRect:CGRectMake(145, 980, 460, 161)];

// 返回繪制的新圖形
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return newImage;
}

IMG_0800.JPG

系統(tǒng)API

/* Called via the -display method when the `contents' property is being
* updated. Default implementation does nothing. The context may be
* clipped to protect valid layer content. Subclasses that wish to find
* the actual region to draw can call CGContextGetClipBoundingBox(). */

- (void)drawInContext:(CGContextRef)ctx;

/** Rendering properties and methods. **/

/* Renders the receiver and its sublayers into 'ctx'. This method
* renders directly from the layer tree. Renders in the coordinate space
* of the layer.
*
* WARNING: currently this method does not implement the full
* CoreAnimation composition model, use with caution. */

- (void)renderInContext:(CGContextRef)ctx;
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末太抓,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子令杈,更是在濱河造成了極大的恐慌走敌,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,451評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件逗噩,死亡現(xiàn)場(chǎng)離奇詭異掉丽,居然都是意外死亡跌榔,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門捶障,熙熙樓的掌柜王于貴愁眉苦臉地迎上來僧须,“玉大人,你說我怎么就攤上這事项炼〉F剑” “怎么了?”我有些...
    開封第一講書人閱讀 164,782評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵锭部,是天一觀的道長(zhǎng)暂论。 經(jīng)常有香客問我,道長(zhǎng)拌禾,這世上最難降的妖魔是什么取胎? 我笑而不...
    開封第一講書人閱讀 58,709評(píng)論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮湃窍,結(jié)果婚禮上闻蛀,老公的妹妹穿的比我還像新娘。我一直安慰自己您市,他們只是感情好觉痛,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,733評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著茵休,像睡著了一般秧饮。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上泽篮,一...
    開封第一講書人閱讀 51,578評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音柑船,去河邊找鬼帽撑。 笑死,一個(gè)胖子當(dāng)著我的面吹牛鞍时,可吹牛的內(nèi)容都是我干的亏拉。 我是一名探鬼主播,決...
    沈念sama閱讀 40,320評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼逆巍,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼及塘!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起锐极,我...
    開封第一講書人閱讀 39,241評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤笙僚,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后灵再,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體肋层,經(jīng)...
    沈念sama閱讀 45,686評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡亿笤,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,878評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了栋猖。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片净薛。...
    茶點(diǎn)故事閱讀 39,992評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖蒲拉,靈堂內(nèi)的尸體忽然破棺而出肃拜,到底是詐尸還是另有隱情,我是刑警寧澤雌团,帶...
    沈念sama閱讀 35,715評(píng)論 5 346
  • 正文 年R本政府宣布燃领,位于F島的核電站,受9級(jí)特大地震影響辱姨,放射性物質(zhì)發(fā)生泄漏柿菩。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,336評(píng)論 3 330
  • 文/蒙蒙 一雨涛、第九天 我趴在偏房一處隱蔽的房頂上張望枢舶。 院中可真熱鬧,春花似錦替久、人聲如沸凉泄。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)后众。三九已至,卻和暖如春颅拦,著一層夾襖步出監(jiān)牢的瞬間蒂誉,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評(píng)論 1 270
  • 我被黑心中介騙來泰國(guó)打工距帅, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留右锨,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,173評(píng)論 3 370
  • 正文 我出身青樓碌秸,卻偏偏與公主長(zhǎng)得像绍移,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子讥电,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,947評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 1蹂窖、通過CocoaPods安裝項(xiàng)目名稱項(xiàng)目信息 AFNetworking網(wǎng)絡(luò)請(qǐng)求組件 FMDB本地?cái)?shù)據(jù)庫(kù)組件 SD...
    陽(yáng)明先生_X自主閱讀 15,981評(píng)論 3 119
  • 在開發(fā)sdk生成jar在eclipse里相對(duì)比較容易操作,只要導(dǎo)出class時(shí)指定哪里導(dǎo)出就可以恩敌,但在用Andro...
    爸比好酷閱讀 3,753評(píng)論 0 1
  • 人生就是這樣從哪跌倒就從哪爬起來青春是人生的一部分但卻是人生的精華基本所有大事小事瞬测,就會(huì)出現(xiàn)在這一階段比如。 ...
    嵐咪閱讀 243評(píng)論 0 0
  • 所有人都以為‘杰出’源于‘天賦‘,‘天才’卻說:我的成就源于‘正確的練習(xí)’涣楷! 著名心理學(xué)家艾利克森在‘專業(yè)特長(zhǎng)科學(xué)...
    王海嶺閱讀 272評(píng)論 0 0