【转】iOS-生成Bundle包-引入bundle-使用bundle
2.添加需要的圖片 加入你需要編譯在bundle中的資源文件。 當然,默認的配置也是可以的,如果你需要特定的優(yōu)化或者特定的路徑配置,你可以進行下面第3步的配置。 3.你可以對編譯的bundle進行一些可選的設置(可選) a.作為資源包,僅僅需要編譯就好,無需安裝相關的配置。 b.同樣要刪除安裝路徑。 c.該資源包的pch文件和strings文件是可以刪除的。 4.最好狀態(tài)下,要編譯出適用與iPhone的bundle文件。 項目集成bundle 使用bundle就非常的easy了,將編譯好的XXXX.bundle 文件直接加入到需要的項目中。省略了! 使用bundle中的資源 將要使用的bundle集成到項目中后,就可以使用了。需要注意的就是,bundle是靜態(tài)的,不進行編譯的資源文件。所以,要使用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?*imgPath= [bundlePath?stringByAppendingPathComponent?:@"img_collect_success.png"];
UIImage?*image_1=[UIImage?imageWithContentsOfFile:imgPath];
[imgView?setImage:image_1];
當然,可以寫成預編譯語句:#define MYBUNDLE_NAME?@?"MyBundle.bundle"
#define MYBUNDLE_PATH?[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: MYBUNDLE_NAME]
#define MYBUNDLE [NSBundle bundleWithPath: MYBUNDLE_PATH]
希望對你有所幫助! from:http://blog.sina.com.cn/s/blog_7b9d64af0101jmj2.html轉載于:https://www.cnblogs.com/xuan52rock/p/5122742.html
總結
以上是生活随笔為你收集整理的【转】iOS-生成Bundle包-引入bundle-使用bundle的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2016预读书单
- 下一篇: 正则表达式获取body内容