2014-02-15 94 views
0

我試着去閱讀文檔的特定文件夾的內容目錄,多數民衆贊成只包含PNG的/Documents/ApplianceImagesFolder/獲取文檔中特定文件夾的內容diectory

目前我只可以從文件夾我的圖片,我怎麼能目標內容只有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

檢查這[post](http://www.techotopia.com/index.php/Working_with_Directories_on_iPhone_OS) – Pawan

回答

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); 
相關問題