2011-09-07 222 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

相关问题