4

I am making an iPhone app using Xcode and following is the directory structure

GFGGAME
  -gfggame.xcodeproj
  -Images
    --bagold
     ---bags_1.png
     ---bags_2.png
    --bagsnew
     ---bags_5.png
     ---bags_6.png

I want to access all images from folder bagsold and bagsnew . If I use resource path and a predicate filter for png it gives me all the png files . Is there a way i can access just the files present in the folder.

ila
  • 920
  • 12
  • 35

4 Answers4

3

I think you probably want to use this NSBundle method:

+ (NSArray *)pathsForResourcesOfType:(NSString *)extension inDirectory:(NSString *)bundlePath

See API docs here

You would pass @".png" as your extension, and then specify the directory path just for the subdirectories you want (you might need to call this twice, one for each subdirectory, and then just append the second array of paths to the first).

See a similar question on Stack Overflow here

Note the point in the answer (link) above, about what you need to do in Xcode to make this work.

Community
  • 1
  • 1
Nate
  • 31,017
  • 13
  • 83
  • 207
  • NSString* bundlePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"/Images"];    NSArray* files = [NSBundle pathsForResourcesOfType:@".png" inDirectory:bundlePath];        for ( NSString* fn in files)        NSLog(@"%@",fn); This is returning me no images. Where as if I don't append images to the resource path I get all the images in all folders – ila Sep 10 '12 at 07:27
  • @ila, I think you have an extra slash (`/`) in your path (`@"/Images"`) ... although that *probably* doesn't hurt. Also, did you note the comment at the end of the answer I linked to (where he says "BUT ...")? Did you do that? – Nate Sep 10 '12 at 08:42
3

From your responses to the other people who have answered, it sounds to me like you've confused folders with Xcode groups. Xcode groups do not correspond to folders on the device. They are solely there to help you keep your Xcode project organised for you, not the device. Any resources just get copied straight into the main directory bundle with a flat hierarchy.

When you drag a folder into Xcode to add it to a project, you need to select "Create folder references for any added folders" and not "Create groups for any added folders". This will preserve the directory layout when the application is built.

Jim
  • 72,985
  • 14
  • 101
  • 108
2

Do this:

NSString *bundleRootpath = [[NSBundle mainBundle] bundlePath];
NSString *filePath = [bundleRootpath pathForResource:@"bags_1" ofType:@"png" inDirectory:@"Images/bagold"]
NSFileManager *fileMangr = [NSFileManager defaultManager];
NSArray *dirContents = [fileMangr contentsOfDirectoryAtPath:bundleRootpath error:nil]; //your path here it may be document directory also

Filter JPG if any

NSArray *onlyJPGs;
if([dirContents count] > 0){
NSPredicate *fltrJPG = [NSPredicate predicateWithFormat:@"self ENDSWITH '.jpg'"];
onlyJPGs = [dirContents filteredArrayUsingPredicate:fltrJPG];
}

Now PNG if any

NSArray *onlyPNGs;
if([dirContents count] > 0){
NSPredicate *fltrPNG = [NSPredicate predicateWithFormat:@"self ENDSWITH '.png'"];
onlyPNGs = [dirContents filteredArrayUsingPredicate:fltrPNG];

Search for any other format if any

Merge onlyJPGs and onlyPNGs into one array

Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
  • I am having a problem getting path to the Images/bagsold folder. How can I get that ? – ila Sep 10 '12 at 07:15
1

Just for variety :)

   NSMutableArray *filePaths = [NSMutableArray arrayWithArray:[NSBundle pathsForResourcesOfType:nil inDirectory:fullPathToFolder]];
    NSMutableArray *toDelete = [NSMutableArray array];
    if(filePaths.count>0){
        [filePaths enumerateObjectsUsingBlock:^(NSString* obj, NSUInteger idx, BOOL *stop) {
            if(!([obj hasSuffix:@"jpg"] || [obj hasSuffix:@"png"])){
                    [toDelete addObject:obj];
            }
        }];
    }
    [filePaths removeObjectsInArray:toDelete];
    return filePaths;
DarkoM
  • 335
  • 4
  • 5