我想在文档目录中创建文件夹,并希望将图像保存在该文件夹中。我试过但文件夹和图像存储在文档目录中。但我想将图像存储在文件夹中。我的代码工作正常,但图像没有保存在文件夹中..
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];
}
答案 0 :(得分:2)
我猜你的“anImagePath”仍然指向文档目录。 将其编辑为“dataPath”作为参数,如下所示
NSString *anImagePath = [NSString stringWithFormat:@"%@/%@", dataPath, anImageName];
答案 1 :(得分:0)
试试这个..
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:YES attributes:nil error:&error];
实际上你在“withIntermediateDirectories”参数中传递NO,并且如果传递NO而不是任何中间父目录不存在。如果任何中间路径元素对应于文件而不是目录,则此方法也会失败。