2011-09-07 153 views
8

我想要獲取資源文件夾子目錄中的所有png文件的數組。 在我的應用程序資源文件夾中,我創建了一個名爲「images」的文件夾。這個文件夾包含我需要顯示的所有png文件。獲取資源文件夾中子目錄的路徑

這是我試圖讓路徑的方式:

NSString *imagepath = [NSString stringWithFormat:@"%@/%@",[[NSBundle mainBundle] bundlePath],@"images/"]; 

NSLog(@"Path to Images: %@", imagepath); 

NSArray * paths = [NSBundle pathsForResourcesOfType: @"png" inDirectory:imagepath]; 
NSMutableArray * allImageNames = [[NSMutableArray alloc] init]; 

for (NSString * path in paths) 
{ 
    if ([[path lastPathComponent] hasPrefix: @"AQ"]) 
     continue; 

    [allImageNames addObject: [path lastPathComponent]]; 
} 

這樣,我得到一個路徑像.../appname.app /圖像

但是,如果我嘗試這樣做的,該數組總是空的。 我在做什麼錯?

格爾茨, Zarak

+0

你是什麼形象的名字?他們喜歡'image.AQ'嗎? – Nekto

+0

圖像名稱就像「banana_icon.png」。但是我使用AQGridView創建了所有文件的庫。 – DevZarak

+0

那你爲什麼要檢查'[[path lastPathComponent] hasPrefix:@「AQ」]'? – Nekto

回答

26

就解決了這個問題。

NSBundle *mainBundle = [NSBundle mainBundle]; 
NSArray *pngs = [mainBundle pathsForResourcesOfType:@".png" 
inDirectory:@"random pictures"]; 

NSLog(@"pngs in my dir:%@", pngs); 

dir結構:「資源/隨機圖片」。

這是有效的,但是,當您將文件添加到「資源」中時,您需要檢查「爲任何文件夾創建文件夾引用」,而不是「爲任何添加的文件夾創建組」。

important step

乾杯!

+1

通過使用另一個解決方案解決了這個問題,但我會牢記這一點,並在另一個項目中試一試。長話短說,你得到了接受。 ;)Thx爲您的答覆。 – DevZarak

+0

沒有爲我工作,我有一個文件夾工作與pdf文件,我試圖訪問文件使用:NSString * inWorkPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@「In-Work」]; NSArray * resContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:inWorkPath error:&error];和resContents返回無 –

+0

您是否在將文件夾添加到項目時選中「創建文件夾引用」? @EshwarChaitanya – codrut

相關問題