从文档目录中检索多个图像 [英] Retrieve multiple images from document directory

查看:58
本文介绍了从文档目录中检索多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文档目录中有一个名为"2"的文件夹.现在,在文件夹"2"中,我有五个图像,分别名为0.png,1.png,2.png,3.png和4.png.我想检索这些图像并保存到数组中.我有代码,但只返回一张图像.

I have folder named "2" in document directory. Now in folder "2", I have five images named 0.png, 1.png, 2.png, 3.png and 4.png. I want to retrieve these images and save into an array. I have code but it returns only one image.

int b=2;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
NSLog(@"%@",paths);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d/0.png",b]];
UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];
NSLog(@"%@",getImagePath);
imge.image=img;

推荐答案

只需使用NSFileManager,就像这样:

Just use NSFileManager, something like this:

[[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error];

它将为您提供一个包含给定路径中所有文件的所有文件路径的数组.

It will give you an array with all the file paths for all files in the given path.

这是从URL数组加载图像的方式.

This is how you load the images from the array of URLs.

NSArray *paths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error];
for (NSURL *url in paths)
{
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
}

这篇关于从文档目录中检索多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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