iPhone / iOS如何在许多文件夹中加载大量图像并在表格视图中显示? [英] iPhone/iOS How to load a lot of image in many folder and show in a table view?

查看:225
本文介绍了iPhone / iOS如何在许多文件夹中加载大量图像并在表格视图中显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我讨厌的问题...

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

What if I have many image , and I want load it in to a table view

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

And show the file name as cell's text , and the preview image is also show in the cell.

当我选择单元格时,它将会推送到下一个视图,显示大尺寸图像。

When I select the cell , it will push to next view , show the big size image.

就是这样。

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

I don't know how to load many folder to an array?

/ ***********编辑*********** /
这是我在
里面设置了很多图像的文件夹

/*********** EDIT ***********/ This is the folder I set many images inside

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

You can see that's only one root folder ...

这是我加载图片的代码

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"

但如果我改变这样的根文件夹怎么办?

But what if I change the root folder like this

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

How to read the image files in the subfolders?

推荐答案

该应用程序在查找捆绑包内的文件时不会遇到问题。在大多数情况下,您的应用开发文件夹的结构与最终产品无关。如果您将图像存储在应用程序包中,系统可以找到它。

The App won't have issues finding files inside the bundle. The structure of your app development folders is irrelevant to the end product, for the most part. If you are storing the images in the apps bundle, the system can find it.

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

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

When I've had to do this as a quicky solution, I created an NSDictionary entry for each photo and stored that inside of an array in userPrefs. I programmatically created thumbnails for each image to utilize in the cell.imageView.image property, and then used an NSDictionary with @"description", @"imageName", and @"imageNameThumbnail" as the keys. You could do the same thing with an NSArray and just call the objectAtIndex, but I prefer the plain text friendliness of dictionaries.

您可以尝试测试返回的路径信息。以下是几行:

You can try testing the path information that is being returned. Here are a couple of lines to try:

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

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

See what the output for these lines is, or if they return nothing.

文件目录为filePath

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

然后,枚举documentsDirectoryPath,它应该递归地读取子文件夹。

Then, enumerate the documentsDirectoryPath, and it should read subfolders recursively.

这篇关于iPhone / iOS如何在许多文件夹中加载大量图像并在表格视图中显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆