我试图读取仅包含png /Documents/ApplianceImagesFolder/
目前我只能从文档文件夹中获取所有图像,如何仅定位ApplianceImagesFolder
的内容?
//gets all png form documents folder
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:nil];
fileList=[[NSMutableArray alloc]init];
for (NSString *dir in dirContents) {
if ([dir hasSuffix:@".png"]) {
NSRange range = [dir rangeOfString:@"."];
NSString *name = [dir substringToIndex:range.location];
if (![name isEqualToString:@""]) {
[fileList addObject:name];
}
}
}
NSLog(@"document folder content list %@ ",fileList);
答案 0 :(得分:1)
构建所需的路径:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *folderPath = [documentsPath stringByAppendingPathComponent:@"ApplianceImagesFolder"];
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderPath error:nil];
找到png文件有更好的方法:
fileList=[[NSMutableArray alloc]init];
for (NSString *filename in dirContents) {
NSString *fileExt = [filename pathExtension];
if ([fileExt isEqualToString:@"png"]) {
[fileList addObject:filename];
}
}
NSLog(@"document folder content list %@ ",fileList);