【问题标题】:iPhone/iOS How to load a lot of image in many folder and show in a table view?iPhone/iOS 如何在多个文件夹中加载大量图像并在表格视图中显示?
【发布时间】:2011-07-04 02:29:06
【问题描述】:

我有烦人的问题...

如果我有很多图像,我想将其加载到表格视图中

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

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

就是这样。

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

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

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

这是我加载图片的代码

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"

)

但是如果我像这样更改根文件夹怎么办

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

【问题讨论】:

    标签: iphone ios nsmutablearray nsarray directory


    【解决方案1】:

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

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

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

    您可以尝试测试正在返回的路径信息。这里有几行可以尝试:

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

    查看这些行的输出是什么,或者它们是否不返回任何内容。

    文件目录作为文件路径

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

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

    【讨论】:

    • 谢谢,这就是我所做的,但我得到了空数据....我添加了一个名为“Image”的文件夹,并在文件夹中添加了许多 png。比我尝试给出一个文件路径 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Image"ofType:@""];但是我不确定我需要设置哪种类型?
    • 我收到了你的代码警告:不兼容的 Objective-C 类型正在初始化 'struct NSString *',预期的 'struct UIImage *' 这是什么意思???
    • 我更新了我的答案。我遗漏了 UIImage 初始化语句,这会导致 UIImage 获取错误的数据类型。继续更新上面的代码,看看我发布的测试代码。
    • 我得到了这个结果路径2:/Users/macminiserver/Library/Application Support/iPhone Simulator/4.3/Applications/5A5B3DF9-5EF5-4B41-B684-EDC84C171253/LoadFolder.app/Image/Image1 可以我得到文件夹名称???这是我的文件夹路径,如:RootFolder ->Folder1,Folder2,Folder3... 并且图像在 Folder1,2,3... 所以我的表格视图将有两层,第一是列出所有文件夹名称,第二列出文件夹中的所有图像。
    • 我假设 Path1 是空白的。如果图像被命名为“Image1”,那么 Path2 语句会正确返回您的图像路径。我的猜测是这些将是 Documents 目录的子文件夹。您可以使用 contentsOfDirectoryAtPath:error: 命令列出文档目录中的文件,然后从那里迭代它返回的数组,检查 isDirectory 并将目录写入另一个单独的数组。然后,您可以使用该列表来提取文件数据。或者,如果目录始终相同,您可以将名称存储在首选项中自己的数组中。
    【解决方案2】:

    如果您使用的是本地数据,您可能希望将数据放在 .plist 文件中。您只需将文件的名称放在那里,然后将 plist 加载到 NSDictionary 中,您将只有一个键用于称为“图像”或类似的东西。然后,您可以将“images”键下的所有对象加载到一个数组中,并使用该数组来填充您的表格。

    希望这是有道理的。

    【讨论】:

    • 是的,它是本地数据。如何将图像添加到 plist 中? Root > Array >Item 0 >Type Data >Value ???
    • 您只想为每个图像添加一个 NSString,它是图像的名称 - 例如 - image123.png。然后当你从数组中读出它来显示它时,你会调用 [UIImage imageNamed:image];其中 "image" 是一个 NSString 保存你的图像的名称。
    • 属性列表只能保存 NSString、NSArray、NSDictionary、NSData、NSDate、NSNumber 或布尔值。
    • 图片太多了……大概5000张……没有别的办法给文件名吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多