objective-cxcodedirectorymainbundle

How to extract immages from a folder in the mainBundle to an array?


In my app bundle, I have several images of several items.

ItemA_largepicture.png

ItemA_smallPicture.png

ItemA_overViewPicture.png

ItemB_largepicture.png

ItemB_smallPicture.png

ItemB_overViewPicture.png

ItemC_largepicture.png

ItemC_smallPicture.png

ItemC_overViewPicture.png

...

I want to extract, for example all ItemB pictures into an array. I can do as Prince suggested

NSString *bundleRootPath = [[NSBundle mainBundle] bundlePath];
NSArray *bundleRootContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundleRootPath error:nil];
NSArray *files = [bundleRootContents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self beginswith 'ItemB'"]];
NSLog(@"%@",files);

This worked very well. The next problem is that all ItemB files is in a folder named ItemB. Somehow I need to add a path within the bundle to the ItemB folder. I thought

NSString *bundleRootPath = [[NSBundle bundleWithPath:@"ItemB"] bundlePath];

was logical, but this didn't work. Can anyone please explain how this works, and how to access the folder?


Solution

  • NSString *bundleRootPath = [[NSBundle mainBundle] bundlePath];
    NSString *itemBPath = [bundleRootPath stringByAppendingPathComponent:@"ItemB"];
    NSArray *itemBContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:itemBPath error:nil];