iOS開發時使用的bundle路徑

來源:互聯網
上載者:User

標籤:data-   直接   --   ant   取圖   class   bsp   沒有   .net   

  bundle是一個目錄,其中包含了程式會使用到的資源. 這些資源套件含了像,聲音,編譯好的代碼,nib檔案(使用者也會把bundle稱為plug-in). 對應bundle,cocoa提供了類NSBundle.我們的程式是一個bundle. 在Finder中,一個應用程式看上去和其他檔案沒有什麼區別. 但是實際上它是一個包含了nib檔案,編譯代碼,以及其他資源的目錄. 我們把這個目錄叫做程式的main bundle。

通過使用下面的方法得到程式的main bundle
NSBundle *myBundle = [NSBundle mainBundle];

一般我們通過這種方法來得到bundle.如果你需要其他目錄的資源,可以指定路徑來取得bundle
NSBundle *goodBundle;
goodBundle = [NSBundle bundleWithPath:@"~/.myApp/Good.bundle"];

一旦我們有了NSBundle 對象,那麼就可以訪問其中的資源了

NSBundle束,是一種特定的檔案類型,其中的內容遵循特定的結構。

NSBundle的一個主要作用是 擷取Resources檔案夾中的資源

 

        在編程中使用[NSData dataWithContentOfFile:@"foo"]的時候,總是無法讀取正確的檔案內容。而使用[NSData dataWithContentOfFile:[[NSBundle mainBundle] pathForResource:@”foo” ofType:@”"]的時候就可以。

因為當使用相對路徑的時候,其實他相對的目前的目錄並不是程式啟動並執行目錄,而是“/”。只有使用[NSBundle mainBundle]來產生的路徑才是檔案真正的路徑。 在此記錄一下:在以後的開發中不直接使用任何相對路徑,而是使用經過計算以後的絕對路徑。 

一.擷取圖片

  1.   NSString *path = [[NSBuddle mainBuddle] pathForResource:@"resourceName" [email protected]"resourceType"];

       UIImage *image = [[UIImage imageWithContentsOfFile:path];

 

  2.   UIImage *image = [UIImage imageNamed:@"imageName"];

二.擷取plist檔案

  NSArray *array =[[NSArrayalloc]initWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"name"ofType:@"plist"]];

  NSDictionary *dict=[arrayobjectAtIndex:index];//將plist檔案中的內容轉換成字典-------轉(http://blog.csdn.net/bihailantian1988/article/details/7703358)

最後記住:iOS開發使用的路徑為bundle路徑

iOS開發時使用的bundle路徑

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.