在资源文件夹中获取子目录的路径 [英] Get Path to Subdirectory in Resources Folder
问题描述
我试图在资源文件夹的子目录中获取所有png文件的数组。
在我的应用程序资源文件夹中,我创建了一个名为images的文件夹。这个文件夹包含了我需要显示的所有png文件。
这是我尝试获取路径的方式:
NSString * imagepath = [NSString stringWithFormat:@%@ /%@,[[NSBundle mainBundle] bundlePath],@images /];
NSLog(@Path to Images:%@,imagepath);
NSArray * paths = [NSBundle pathsForResourcesOfType:@pnginDirectory:imagepath];
NSMutableArray * allImageNames = [[NSMutableArray alloc] init];
for(NSString * path in paths)
{
if([[path lastPathComponent] hasPrefix:@AQ])
continue;
[allImageNames addObject:[path lastPathComponent]];
}
这样我得到一个像... / appname.app / images
但是如果我尝试这样做,数组总是空的。
我做错了什么?
greetz,
Zarak
刚刚解决了。
NSBundle * mainBundle = [NSBundle mainBundle];
NSArray * pngs = [mainBundle pathsForResourcesOfType:@。png
inDirectory:@random pictures];
NSLog(@pngs in my dir:%@,pngs);
dir结构:资源/随机图片。
这样做,但是当您将文件添加到资源时,您需要选中为任何已添加文件夹创建文件夹引用,而不是为任何添加的文件夹创建组 。
干杯!
i'm trying to get an array of all png files in a subdirectory of the Resources Folder. In my apps Resources Folder I have created a Folder named "images". This folder holds all the png-files I need to display.
This is the way I tried to get the 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]];
}
This way I get a Path like …/appname.app/images
But if i try to do it that way, the array is always empty. What am I doing wrong?
greetz, Zarak
Just solved it.
NSBundle *mainBundle = [NSBundle mainBundle];
NSArray *pngs = [mainBundle pathsForResourcesOfType:@".png"
inDirectory:@"random pictures"];
NSLog(@"pngs in my dir:%@", pngs);
dir structure: "Resources/random pictures".
This works, BUT, when you add the files to 'Resources', you need to check "Create folder references for any aded folders", and NOT "Create groups for any added folders".
Cheers!
这篇关于在资源文件夹中获取子目录的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!