【发布时间】:2015-11-05 09:10:11
【问题描述】:
我想将多个图像/视频保存到文档目录以供以后使用,并且我了解了如何保存单个图像的要点,但我不知道如何保存多个图像并检索它们。我将如何更改此代码以检索保存在文件所在目录中的所有对象?我正在使用两个视图控制器来执行此操作。
这是保存图片的代码:
NSData *videoData = [NSData dataWithContentsOfFile:moviePath];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
filePath = [documentsPath stringByAppendingPathComponent:@"image.png"]; //Add the file name
[videoData writeToFile:filePath atomically:YES]; //Write the file
这是获取图片的代码:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:@"image.png"];
UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];
代码的唯一问题是这只会获取第 0 个位置的图像,我想获取所有位置。
【问题讨论】:
-
如果您可以为一个文件执行此操作,您可以为多个文件执行此操作,使用简单的
for...
标签: ios arrays file nsdocumentdirectory