-1

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.

Neeku
  • 3,646
  • 8
  • 33
  • 43
Peer Mohamed Thabib
  • 636
  • 2
  • 9
  • 29
  • 1
    See my answer here http://stackoverflow.com/questions/24389720/can-i-access-all-images-in-a-xcassets-at-once/24390080#24390080 – iphonic Aug 05 '14 at 10:37

4 Answers4

2

it's simple to get all the images into a array

NSBundle *imageBundle=[NSBundle bundleWithPath: myBundlePath];
NSArray *allImageURLs=[imageBundle URLsForResourcesWithExtension:@"png" subdirectory:nil];
Retro
  • 3,985
  • 2
  • 17
  • 41
1

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

NSArray *imagesArray = [[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:@"Images"];
The dude
  • 7,896
  • 1
  • 25
  • 50
0

You can try below

NSArray *pathArray = [[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:nil];

[pathArray enumerateObjectsUsingBlock:^(NSString *pathString, NSUInteger idx, BOOL *stop) {

    UIImage *yourImage = [[UIImage alloc] initWithContentsOfFile:pathString];
}];
Tapas Pal
  • 7,073
  • 8
  • 39
  • 86
0

Please use this code, hope it will work.

    NSString *dirPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"images"];
    NSError * error;
    NSArray * images =  [[NSFileManager defaultManager]
                  contentsOfDirectoryAtPath:dirPath error:&error];
Sreejith Bhatt
  • 589
  • 3
  • 12