获取文档目录中特定文件夹的内容

时间:2014-02-15 20:06:43

标签: ios objective-c cocoa-touch

我试图读取仅包含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);

1 个答案:

答案 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);