8

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

Mike Hay
  • 2,828
  • 21
  • 26
DevZarak
  • 269
  • 1
  • 5
  • 20

1 Answers1

27

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!

codrut
  • 810
  • 10
  • 19
  • 1
    Solved the problem by using another solution, but i'll keep this in mind and give it a try in another project. Long story short, you get the accept. ;) Thx for your reply. – DevZarak Mar 06 '12 at 20:56
  • Didn't work for me, I have a folder In-Work with pdf files, I am trying to access the files using: NSString *inWorkPath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"In-Work"];NSArray *resContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:inWorkPath error:&error]; and resContents return nil – Eshwar Chaitanya Dec 24 '14 at 19:07
  • did you check "Create folder references" when adding the folder to the project? @EshwarChaitanya – codrut Jan 05 '15 at 16:24
  • @codrut Oops, it was my mistake. Like you said, I didn't check Create folder references while copying the resources. – Eshwar Chaitanya Jan 06 '15 at 04:46