Reputation: 920
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.
Upvotes: 4
Views: 5677
Reputation: 335
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;
Upvotes: 1
Reputation: 31045
I think you probably want to use this NSBundle
method:
+ (NSArray *)pathsForResourcesOfType:(NSString *)extension inDirectory:(NSString *)bundlePath
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.
Upvotes: 3
Reputation: 73936
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.
Upvotes: 3
Reputation: 38239
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
Upvotes: 2