I need to access all jpg,png,bmp in my Resource开发者_运维技巧s 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...
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]);
}
You can specify nil
for the extension to retrieve all bundle resources. Then, in the for
loop, check for [filename pathExtension]
.
精彩评论