Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

ios - Get Path to Subdirectory in Resources Folder

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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".

important step

Cheers!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...