我正在尝试将文件保存在文档目录中,在批量操作中我正在使用此方法:
NSURL *url = [NSURL URLWithString:@"http://www.mywebsite.com/storage/image.png"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *downloadFolder = [documentsPath stringByAppendingPathComponent:@"image.png"];
[data writeToFile:downloadFolder atomically:TRUE];
这样做效果很好,但是对于单个文件,我想同时复制文档目录中STORAGE文件夹中的所有文件。
有谁知道我该怎么做?
感谢。
答案 0 :(得分:0)
如果您只想将图像批量保存在文档目录中。
您可以使用 NSUserDefaults 保存最后一个图像索引,并在存储新图像时将索引增加1,试试这个。
NSURL *url = [NSURL URLWithString:@"http://www.mywebsite.com/storage/image.png"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *filePath;
id index = [[NSUserDefaults standardUserDefaults] objectForKey:@"imageIndex"];
if(index){
int i = [((NSNumber *) index) intValue];
i ++;
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:i] forKey:@"imageIndex"];
filePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"image_%d.png",i]];
}else{
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:0] forKey:@"imageIndex"];
filePath = [documentsPath stringByAppendingPathComponent:@"image_0.png"];
}
[data writeToFile:filePath atomically:TRUE];