Access all images in iphone application folder

I am making an iPhone application using Xcode, and below 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 the images from the bagsold and bagsnew . If I use the resource path and predicate filter for png, it gives me all png files. Is there any way to access the files in the folder.

+4
source share
4 answers

I think you probably want to use this NSBundle method:

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

See here API

You will pass @".png" as an extension, and then specify the directory path only for the subdirectories you want (you may need to call this twice, one for each subdirectory, and then just add a second array of paths to the first).

See a similar stack overflow question here

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

+3
source

From your answers to other people who answered, this sounds to me as if you are confusing folders with Xcode groups. Xcode groups do not match folders on the device. They are intended solely to support your Xcode project for you, not the device. Any resources are simply copied directly to the main directory with a flat hierarchy.

When you drag a folder into Xcode to add it to the project, you need to select "Create folder links for any added folders" and not "Create groups for any added folders." This will save the catalog layout when creating the application.

+3
source

Do it:

 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 

JPG filter, 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 only JPG and onlyPNG into one array

+2
source

Just for a change :)

  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; 
+1
source

Source: https://habr.com/ru/post/1433299/


All Articles