Search code examples
iosobjective-cresourcebundlensbundle

How To Read Images From NSMainBundle in iOS


I have the structure like the following in my Xcode project.

enter image description here

I need to read all the images from project and need to store it in array.

I have used the following code from some other StackOverflow posts, but it's not working.

NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Images"ofType:@""];
NSDirectoryEnumerator *direnum;
direnum = [fileManager enumeratorAtPath: filePath];
imageFolder = [NSMutableArray new];
for(NSString *filename in direnum){
    if([[filename pathExtension] isEqualToString:@"png"]){
        [imageFolder addObject:filename];
    }
}
NSLog(@"Files in the folder %@",imageFolder);

I would appreciate, if any one help me to complete this.


Solution

  • - (NSArray *)pathsForResourcesOfType:(NSString *)extension inDirectory:(NSString *)subpath will get you the array you want.

    NSArray *imagesArray = [[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:@"Images"];