在资源文件夹中获取子目录的path

我试图获得资源文件夹的子目录中的所有PNG文件的数组。 在我的应用程序资源文件夹中,我创build了一个名为“images”的文件夹。 这个文件夹包含我需要显示的所有png文件。

这是我试图获得path的方式:

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 / images的path

但是,如果我尝试这样做,数组总是空的。 我究竟做错了什么?

greetz,Zarak

刚刚解决它。

 NSBundle *mainBundle = [NSBundle mainBundle]; NSArray *pngs = [mainBundle pathsForResourcesOfType:@".png" inDirectory:@"random pictures"]; NSLog(@"pngs in my dir:%@", pngs); 

目录结构:“资源/随机图片”。

这工作,但是,当您将文件添加到“资源”,您需要选中“ 创build任何文件夹的文件夹引用 ”,而不是“为任何添加的文件夹创build组”。

重要的一步

干杯!