bundle是一個目錄,其中包含了程式會使用到的資源.這些資源包含了如圖像,聲音,編譯好的代碼,nib檔案(使用者也會把bundle稱為plug-in).對應bundle,
cocoa提供了類NSBundle.
我們現在用bundle擷取程式裡的一張圖檔,并顯示到View上。
建立一個Single View Application,并在加入viewDidLoad方法裡加入如下代碼:
// 通過使用下面的方法得到程式的main bundle
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *imagePath = [mainBundle pathForResource:@"QQ20120616-1" ofType:@"png"];
NSLog(@"%@", imagePath);
UIImage *image = [[UIImage alloc]initWithContentsOfFile:imagePath];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
在項目上右鍵,add圖檔檔案圖檔檔案QQ20120616.png。
運作程式:
列印出來圖檔路徑如下
/Users/rongfzh/Library/Application Support/iPhone Simulator/5.1/Applications/3B8EC78A-5EEE-4C2F-B0CB-4C3F02B996D2/iOSSandbox.app/QQ20120616-1.png
我們可以看到,圖檔在iOSSandbox.app這個包裡,
圖檔顯示:

圖檔取出來并顯示了。
如何聯系我:【萬裡虎】www.bravetiger.cn
【QQ】3396726884 (咨詢問題100元起,幫助解決問題500元起)
【部落格】http://www.cnblogs.com/kenshinobiy/