iPhone / iOS如何在许多文件夹中加载大量图像并显示在表格视图中?

我讨厌的问题…

如果我有很多图像,我想把它加载到一个表格视图

并将文件名称显示为单元格文本,预览图像也显示在单元格中。

当我select单元格时,它会推到下一个视图,显示大尺寸的图像。

而已 。

我不知道如何将多个文件夹加载到数组?

/ ***********编辑*********** /这是我在里面设置了许多图像的文件夹 在这里输入图像说明

你可以看到只有一个根文件夹…

这是我的代码来加载内部的图像

NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Image"ofType:@""]; NSDirectoryEnumerator *direnum; direnum = [fileManager enumeratorAtPath: filePath]; imageFolder = [NSMutableArray new]; for(NSString *filename in direnum){ if([[filename pathExtension] isEqualToString:@"png"]){ [imageFolder addObject:filename]; } } NSLog(@"Files in the folder %@",imageFolder); 

我得到这样的结果:

  Files in the folder ( "macro1.png", "macro10.png", "macro11.png", "macro12.png", "macro13.png", "macro14.png", "macro15.png", "macro16.png", "macro17.png", "macro18.png", "macro19.png", "macro2.png", "macro20.png", "macro21.png", "macro22.png", "macro23.png", "macro24.png", "macro25.png", "macro26.png", "macro27.png", "macro4.png", "macro5.png", "macro6.png", "macro7.png", "macro8.png", "macro9.png" 

但是,如果我改变这个根文件夹呢? 在这里输入图像说明

如何读取子文件夹中的图像文件?

该应用程序将不会有问题find文件中的文件。 大多数情况下,应用程序开发文件夹的结构与最终产品无关。 如果要将图像存储在应用程序包中,则系统可以find它。

 UIImage *image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"imageName" ofType:@"png"]]; 

当我不得不做这个快速解决scheme时,我为每张照片创build了一个NSDictionary条目,并将其存储在userPrefs中的一个数组内。 我以编程方式为每个图像创build缩略图,以便在cell.imageView.image属性中使用,然后使用带有@“description”,@“imageName”和@“imageNameThumbnail”作为关键字的NSDictionary。 你可以用NSArray做同样的事情,只需调用objectAtIndex,但我更喜欢字典的纯文本友好性。

您可以尝试testing正在返回的path信息。 这里有几条线来尝试:

 NSString *path1 = [[NSBundle mainBundle] pathForResource:@"imageName" ofType:@"png"]; NSString *path2 = [[NSBundle mainBundle] pathForResource:@"imageName" ofType:@"png" inDirectory:@"folderName"]; NSLog(@"Path 1: %@",path1); NSLog(@"Path 2: %@",path2); 

看看这些线路的输出是什么,或者它们什么都不返回。

Documents目录为filePath

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectoryPath = [paths objectAtIndex:0]; 

然后,枚举documentsDirectoryPath,它应recursion读取子文件夹。

如果它是您正在使用的本地数据,则可能需要将数据放在.plist文件中。 你只需要把文件的名字放在那里,然后把plist加载到一个NSDictionary中,你只需要一个关键字叫做“images”或类似的东西。 然后,您可以将“图像”键下的所有对象加载到数组中,然后使用该数组填充表格。

希望这是有道理的。