Accessing all images in a folder iphone Application

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.

Answers


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.


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.


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


Need Your Help

ggplot2 add a legend for several stat_functions

r ggplot2 legend

I see a lot of questions regarding how to customize legends, but I can't even get a legend to customize. I would like to have a legend explaining that the black line is quadratic and that the green...

Hover all child elements with Jquery

javascript jquery html css css3

Hey guys i have a div that contains an image and a <figure> with a <figcaption>. I want it so when someone mouses over the box it fires all of the hover effects. any ideas? here is how ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.