[原創]http://www.cnblogs.com/luoguoqiang1985
以前,自己寫程序,圖片等資源放得比較亂。后來,發現有個更好的方法來管理圖片等資源文件 --bundle文件。
1)怎么制作bundle文件
其實很簡單,先新建一個文件,把資源文件放進去,接着,重命名文件為.bundle類型文件。
真相:
是不是很簡單?哈哈
2)bundle文件在ios下的使用
對bundle文件的使用,ios 提供了NSBundle類。
長話短說,看招~~~~
1 /* 2 * 根據枚舉獲取資源 3 */ 4 - (NSString *) getResourceByEnum:(nResources) resName{ 5 NSString *result = nil; 6 NSString *bundlePath = [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:LGQ_BUNDLE]; 7 NSBundle *bundle = [NSBundle bundleWithPath:bundlePath]; 8 switch (resName) { 9 case nLuoJunRui: 10 result = [bundle pathForResource:@"ljr" ofType:@"JPG"]; 11 break; 12 13 default: 14 break; 15 } 16 17 return result; 18 }
上面這段代碼是我的測試代碼,里面有幾個關鍵點:
1)生成NSBundle實例
[NSBundle mainBundle] - 獲得當前可執行APP目錄的對象
NSBundle的resourcePath屬性,它是一個NSString 類型。它代表完整路徑名字。
resourcePath 的 stringByAppendingPathComponent ,通過附加astring傳遞給接收方生成一個新的字符串,前面如果有必要通過一個路徑分隔符。
[NSBundle bundleWithPath:bundlePath] 生成一個指定目錄的NSBundle對象