gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:52:16 24 4
gpt4 key购买 nike

我有一个烦人的问题...

如果我有很多图片,我想将它加载到一个 TableView 中怎么办

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

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

就是这样。

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

/*********** 编辑 ***********/这是我在里面放了很多图片的文件夹 enter image description here

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

这是我加载图片的代码

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"

)

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

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

最佳答案

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

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,它应该递归地读取子文件夹。

关于iPhone/iOS 如何在多个文件夹中加载大量图像并在表格 View 中显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6566827/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com