如何使用路径ForResource OfType获取多种类型的文件

时间:2011-02-19 00:05:27

标签: iphone

我需要访问我的Resources文件夹中的所有jpg,png,bmp,但是 做以下只给我jpg ... 它在算法上是错误的吗? 还是有一个更短,更简单的语法,让我一次访问所有三个? 请帮帮我......

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]);
}
路径2和路径3的

等等......

2 个答案:

答案 0 :(得分:2)

@Ole Begemann解决方案的问题在于你将通过大量不是图像的文件来重复。我认为@CosmicRabbitMediaInc与可变阵列在正确的轨道上,但没有坚持到底。所以:

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]);
}

答案 1 :(得分:1)

您可以为扩展指定nil以检索所有捆绑资源。然后,在for循环中,检查[filename pathExtension]