0

我想在文档目录中创建文件夹并希望将图像保存在该文件夹中。我已经尝试过,但文件夹和图像存储在文档目录中..但我想将图像存储在文件夹中。我的代码工作正常,但图像没有保存在文件夹中..

NSError *error;
NSString *aDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *dataPath = [aDocumentsDirectory stringByAppendingPathComponent:@"Folder"];
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];

NSInteger anIndex = 0;

for (UIImage *anImage in _chosenImages) 
{

NSString *anImageName = [NSString stringWithFormat:@"%d.png", anIndex++];
NSString *anImagePath = [NSString stringWithFormat:@"%@/%@", aDocumentsDirectory, anImageName];
NSLog(@"the path is=%@",anImagePath);

NSData *anImageData = UIImagePNGRepresentation(anImage);
[anImageData writeToFile:anImagePath atomically:YES];
}
4

2 回答 2

2

我猜你的“anImagePath”仍然指向文档目录。将其编辑为“dataPath”作为参数,如下所示

NSString *anImagePath = [NSString stringWithFormat:@"%@/%@", dataPath, anImageName];
于 2013-07-18T09:38:40.377 回答
0

尝试这个..

[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:YES attributes:nil error:&error];

实际上,您在“withIntermediateDirectories”参数中传递了 NO,并且如果您将 NO 传递给此参数,则比任何中间父目录不存在。如果任何中间路径元素对应于文件而不是目录,则此方法也会失败。

于 2013-07-18T10:00:12.623 回答