iOS 把圖片資源打包成bundle

原文
iOS 把圖片資源打包成bundle
iOS_Bundle資源文件包

1. 創(chuàng)建bundle伴挚,如圖,點(diǎn)擊 + ,彈出選擇框砾层, macOS 下的Framework & Library 托慨,點(diǎn)擊bundle左胞,輸入bundle的名字瑰抵,然后點(diǎn)擊 finish你雌。

bundle1.jpeg
bundle2.jpeg

2. 點(diǎn)擊創(chuàng)建好的bundle ,修改屬性

bundle3.jpeg

"Base SDK" 設(shè)置為 "Latest iOS (iOS 11.2)" (Xcode 9.2為例)

"Build Active Architecture Only" 設(shè)置為 "YES"

Installation Directiotory 刪除掉后面的路徑

Code Signing Identity 選擇 Don't Code Sign

"iOS Deployment Target" 設(shè)置為 iOS 8.0 (為了兼容性二汛,最好選擇最低版本)

"Skip Install" 設(shè)置為 "NO"

"Strip Debug Symbols During Copy" 中"Release"模式設(shè)置為 "YES"

"IOS Deployment Target" 設(shè)置為 "iOS 8.0"

"COMBINE_HIDPI_IMAGES" 設(shè)置為 "NO"

3. 現(xiàn)在開始導(dǎo)入資源婿崭,可以導(dǎo)入Xib文件和圖片,此處以導(dǎo)入圖片為例习贫,點(diǎn)擊?進(jìn)行添加逛球,如圖3.1;或者直接將圖片拖入左側(cè)創(chuàng)建的bundle文件夾下苫昌,系統(tǒng)會(huì)自動(dòng)導(dǎo)入Copy Bundle Resources里去,如張圖3.2所示:

bundle4.jpeg
bundle5.jpeg

4. 選擇創(chuàng)建的bundle 進(jìn)行編譯幸海,開始生成bundle祟身,分別選擇真機(jī)和模擬器,然后各運(yùn)行一遍物独,即可生成真機(jī)和模擬器使用的bundle:

bundle6.jpeg

5. 找到生成的bundle袜硫,打包上架APP的時(shí)候應(yīng)使用真機(jī)模式下運(yùn)行生成的Bundle,即Debug-iPhoneos 文件夾內(nèi)的bundle挡篓。

bundle7.jpeg
bundle8.jpeg

現(xiàn)在已經(jīng)生成了我們需要的bundle婉陷,在項(xiàng)目中直接導(dǎo)入即可使用,

6. 將要使用的bundle集成到項(xiàng)目中后官研,就可以使用了秽澳。需要注意的就是,bundle是靜態(tài)的戏羽,不進(jìn)行編譯的資源文件担神。所以,要使用bundle中的資源始花,就需要找到相應(yīng)的資源路徑妄讯。

VC獲得bundle中的資源

NSString * bundlePath = [[ NSBundle mainBundle] pathForResource: @ "MyBundle"ofType :@ "bundle"];
 
NSBundle *resourceBundle = [NSBundle bundleWithPath:bundlePath];

UIViewController *vc = [[UIViewController alloc] initWithNibName:@"vc_name"bundle:resourceBundle];

圖片獲得bundle中的資源

UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 50,50)];
 
UIImage *image = [UIImage imageNamed:@"MyBundle.bundle/img_collect_success"];
 
[imgView setImage:image];

或者

UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 50,50)];
 
NSString *bundlePath = [[ NSBundle mainBundle] pathForResource:@"Test" ofType :@"bundle"];
 
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
 
NSString *img_path = [bundle pathForResource:imageName ofType:@"png"];
 
UIImage *image_1=[UIImage imageWithContentsOfFile:img_path];
 
[imgView setImage:image_1];

當(dāng)然,可以寫成預(yù)編譯語句:

#define MYBUNDLE_NAME @ "MyBundle.bundle"
#define MYBUNDLE_PATH [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: MYBUNDLE_NAME]
#define MYBUNDLE [NSBundle bundleWithPath: MYBUNDLE_PATH]

#define MYBUNDLE_NAME_2   @"BundleTest2.bundle"
#define MYBUNDLE_PATH_2   [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: MYBUNDLE_NAME_2]
#define MYBUNDLE_2        [NSBundle bundleWithPath: MYBUNDLE_PATH_2]

#define MYBUNDLE_NAME_3   @"Bundle3.bundle"
#define MYBUNDLE_PATH_3   [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: MYBUNDLE_NAME_3]
#define MYBUNDLE_3        [NSBundle bundleWithPath: MYBUNDLE_PATH_3]
// 從bundle中加載xib
    UIView *view = [[MYBUNDLE_2 loadNibNamed:@"MyView" owner:self options:nil] objectAtIndex:0];
    view.frame = self.view.frame;
    [self.view addSubview:view];
    
    // 從bundle中加載圖片
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    imageView.image = [UIImage imageWithContentsOfFile:[MYBUNDLE_PATH_2 stringByAppendingPathComponent:@"Contents/Resources/share_pengyouquan"]];
    [view addSubview:imageView];
    
    // 從另一個(gè)bundle中加載storyboard
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Bundle3Storyboard" bundle:MYBUNDLE_3];
    NSLog(@"storyboard = %@",storyboard);
    
    // 從另一個(gè)bundle中加載圖片
    UIImageView *imageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(250, 100, 100, 100)];
    imageView2.image = [UIImage imageWithContentsOfFile:[MYBUNDLE_PATH_3 stringByAppendingPathComponent:@"Contents/Resources/guanyudingyue"]];
    [view addSubview:imageView2];

7. 如果將自己打包的bundle給別人使用酷宵,別人在打包上傳過程中可能會(huì)遇到錯(cuò)誤提示如:

ERROR ITMS-90171: "Invalid Bundle Structure - The binary file 'lhby.app/Test.bundle/Test' is not permitted. Your app can’t contain standalone executables or libraries, other than a valid CFBundleExecutable of supported bundles. Refer to the Bundle Programming Guide at...

或者

ERROR ITMS-90535: "Unexpected CFBundleExecutable Key. The bundle at 'Payload/dianlan2.app/EaseUIResource.bundle' does not contain a bundle executable. If this bundle intentionally does not contain an executable, consider removing the CFBundleExecutable key from its Info.plist and using a CFBundlePackageType of BNDL. If this bundle is part of a third-party framework, consider contacting the developer of the framework for an update to address this issue."

或者

ERROR ITMS-90034: "Missing or invalid signature. The bundle 'ABC.Test' at bundle path 'Payload/lhby.app/Test.bundle' is not signed using an Apple submission certificate."

網(wǎng)上也有很多的解決辦法亥贸,這里提供一種解決方法,就是刪除bundle里的執(zhí)行文件:找到工程中的Test.Bundle浇垦,右鍵單擊后 選擇 "顯示包內(nèi)容"炕置,找到里面黑色的可執(zhí)行文件Test,刪除掉,然后找到里面的info.plist文件 讹俊,刪除掉Executable file 字段垦沉,重新打包,上傳應(yīng)用商店就可以了仍劈。

參考文章:
iOS 把圖片資源打包成bundle
iOS_Bundle資源文件包
【Xcode小技巧】生成Bundle包
iOS-生成Bundle包-引入bundle-使用bundle

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末厕倍,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子贩疙,更是在濱河造成了極大的恐慌讹弯,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,590評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件这溅,死亡現(xiàn)場離奇詭異组民,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)悲靴,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評論 3 399
  • 文/潘曉璐 我一進(jìn)店門臭胜,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人癞尚,你說我怎么就攤上這事耸三。” “怎么了浇揩?”我有些...
    開封第一講書人閱讀 169,301評論 0 362
  • 文/不壞的土叔 我叫張陵仪壮,是天一觀的道長。 經(jīng)常有香客問我胳徽,道長积锅,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,078評論 1 300
  • 正文 為了忘掉前任养盗,我火速辦了婚禮缚陷,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘爪瓜。我一直安慰自己蹬跃,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,082評論 6 398
  • 文/花漫 我一把揭開白布铆铆。 她就那樣靜靜地躺著蝶缀,像睡著了一般。 火紅的嫁衣襯著肌膚如雪薄货。 梳的紋絲不亂的頭發(fā)上翁都,一...
    開封第一講書人閱讀 52,682評論 1 312
  • 那天,我揣著相機(jī)與錄音谅猾,去河邊找鬼柄慰。 笑死鳍悠,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的坐搔。 我是一名探鬼主播藏研,決...
    沈念sama閱讀 41,155評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼概行!你這毒婦竟也來了蠢挡?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,098評論 0 277
  • 序言:老撾萬榮一對情侶失蹤凳忙,失蹤者是張志新(化名)和其女友劉穎业踏,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體涧卵,經(jīng)...
    沈念sama閱讀 46,638評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡勤家,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,701評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了柳恐。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片伐脖。...
    茶點(diǎn)故事閱讀 40,852評論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖胎撤,靈堂內(nèi)的尸體忽然破棺而出晓殊,到底是詐尸還是另有隱情,我是刑警寧澤伤提,帶...
    沈念sama閱讀 36,520評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站认烁,受9級特大地震影響肿男,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜却嗡,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,181評論 3 335
  • 文/蒙蒙 一舶沛、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧窗价,春花似錦如庭、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至帝牡,卻和暖如春往毡,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背靶溜。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評論 1 274
  • 我被黑心中介騙來泰國打工开瞭, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留懒震,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,279評論 3 379
  • 正文 我出身青樓嗤详,卻偏偏與公主長得像个扰,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子葱色,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,851評論 2 361