標籤:
bundle是一個目錄,其中包含了程式會使用到的資源.這些資源套件含了像,聲音,編譯好的代碼,nib檔案(使用者也會把bundle稱為plug-in).對應bundle,
cocoa提供了類NSBundle.
我們現在用bundle擷取程式裡的一張圖片,並顯示到View上。
建立一個Single View Application,並在加入viewDidLoad方法裡加入如下代碼:
[cpp] view plaincopy
- // 通過使用下面的方法得到程式的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這個包裡,
圖片顯示:
圖片取出來並顯示了。
轉自:http://blog.csdn.net/totogo2010/article/details/7672271
【轉】iOS學習之NSBundle介紹和使用