我正在缓存目录中存储图像文件。稍后,我想从缓存目录中获取所有图像文件列表。我使用下面的代码来获取所有文件。
[fileManager contentsOfDirectoryAtPath:pathForCacheDirectory error:&error]如何将图像文件与此分离。图像文件可以是任何格式。
提前谢谢。
发布于 2013-03-22 14:23:57
// Store your supported image Extensions
NSArray *extensionList = [NSArray arrayWithObjects:@"jpg", @"jpeg", @"png", @"gif", @"bmp", nil];
// Grab the content Directory
NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:pathForCacheDirectory error:&error];
NSMutableArray *listOfImageFiles = [NSMutableArray arrayWithCapacity:0];
// Check for Images of supported type
for(NSString *filepath in contents){
if ([extensionList containsObject:[filepath pathExtension]])
{
// Found Image File
[listOfImageFiles addObject:filepath];
}
}
NSLog(@"Lisf of Image Files : %@",listOfImageFiles);发布于 2013-03-22 14:05:26
一种残酷的方式是枚举所有你认为它是一个图像的扩展。一个更好的方法是使用UTI,检查这个Get the type of a file in Cocoa
发布于 2013-03-22 14:11:39
您可以使用扩展名过滤文件。
NSArray *contents = [fileManager contentsOfDirectoryAtPath:pathForCacheDirectory error:&error];
for(NSString *filepath in contents){
if ([[filepath pathExtension] isEqualToString: @"png"]) {
// Your code
}
}https://stackoverflow.com/questions/15563714
复制相似问题