CosmicRabbitMediaInc
CosmicRabbitMediaInc

Reputation: 1175

how to get multiple types of file using pathsForResourcesOfType

I need to access all jpg,png,bmp in my Resources folder, but doing the following only gives me jpg... is it algorithmically wrong? or is there a shorter, simpler syntax that makes me access all three at once? Please help me out..

NSMutableArray *paths = [[[NSBundle mainBundle] pathsForResourcesOfType:@"jpg" inDirectory:nil] mutableCopy];
NSMutableArray *paths2 = [[[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:nil] mutableCopy];
NSMutableArray *paths3 = [[[NSBundle mainBundle] pathsForResourcesOfType:@"bmp" inDirectory:nil] mutableCopy];

for(NSString *filename in paths)
{
    filename = [filename lastPathComponent];
    NSLog(@" filename is %@", filename);
    [filenames addObject:filename];
    NSLog(@" filenames array length is now %d", [filenames count]);
}

and so on for paths2 and paths3...

Upvotes: 1

Views: 1969

Answers (2)

Elise van Looij
Elise van Looij

Reputation: 4232

The problem with @Ole Begemann's solution is that you will be reiterating through a lot of files that aren't images. I think @CosmicRabbitMediaInc was on the right track with the mutable array, but didn't follow through. So:

NSMutableArray *paths = [NSMutableArray arrayWithArray:[[NSBundle mainBundle] pathsForResourcesOfType:@"jpg" inDirectory:nil]];
[paths addObjectsFromArray:[[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:nil]];
[paths addObjectsFromArray:[[NSBundle mainBundle] pathsForResourcesOfType:@"bmp" inDirectory:nil]];

for(NSString *filename in paths)
{
    filename = [filename lastPathComponent];
    NSLog(@" filename is %@", filename);
    [filenames addObject:filename];
    NSLog(@" filenames array length is now %d", [filenames count]);
}

Upvotes: 2

Ole Begemann
Ole Begemann

Reputation: 135548

You can specify nil for the extension to retrieve all bundle resources. Then, in the for loop, check for [filename pathExtension].

Upvotes: 1

Related Questions