iOS 把圖片資源打包成bundle

1. 創建bundle,如圖,點擊 +  ,彈出選擇框, macOS 下的Framework & Library  ,點擊bundle,輸入bundle的名字,然後點擊 finish。

圖1.1

圖1.2

2. 點擊創建好的bundle ,修改屬性

 

圖2.1

"Base SDK" 設置爲 "Latest iOS (iOS 11.2)" (Xcode 9.2爲例)

"Build Active Architecture Only" 設置爲 "YES"

Installation Directiotory    刪除掉後面的路徑

Code Signing Identity   選擇 Don't Code Sign  

"iOS Deployment Target" 設置爲 iOS 8.0  (爲了兼容性,最好選擇最低版本)

"Skip Install" 設置爲 "NO"

"Strip Debug Symbols During Copy" 中"Release"模式設置爲 "YES"

"IOS Deployment Target" 設置爲 "iOS 8.0"

"COMBINE_HIDPI_IMAGES" 設置爲 "NO"

 

3. 現在開始導入資源,可以導入Xib文件和圖片,此處以導入圖片爲例,點擊➕進行添加,如圖3.1;或者直接將圖片拖入左側創建的bundle文件夾下,系統會自動導入Copy Bundle Resources裏去,如張圖3.2所示:

3.1

 

4. 選擇創建的bundle 進行編譯,開始生成bundle,分別選擇真機和模擬器,然後各運行一遍,即可生成真機和模擬器使用的bundle:

 

圖4.1

5. 找到生成的bundle,打包上架APP的時候應使用真機模式下運行生成的Bundle,即Debug-iPhoneos 文件夾內的bundle。

 

圖5.1

 

現在已經生成了我們需要的bundle,在項目中直接導入即可使用,

6. 將要使用的bundle集成到項目中後,就可以使用了。需要注意的就是,bundle是靜態的,不進行編譯的資源文件。所以,要使用bundle中的資源,就需要找到相應的資源路徑。

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];

當然,可以寫成預編譯語句:

#define MYBUNDLE_NAME @ "MyBundle.bundle"

#define MYBUNDLE_PATH [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: MYBUNDLE_NAME]

#define MYBUNDLE [NSBundle bundleWithPath: MYBUNDLE_PATH]

 

 

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

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."

網上也有很多的解決辦法,這裏提供一種解決方法,就是刪除bundle裏的執行文件:找到工程中的Test.Bundle,右鍵單擊後 選擇 "顯示包內容",找到裏面黑色的可執行文件Test,刪除掉,然後找到裏面的info.plist文件 ,刪除掉Executable file 字段,重新打包,上傳應用商店就可以了。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章