【问题标题】:save picture in folder which is located in document direcotory in iphone将图片保存在位于iphone文档目录中的文件夹中
【发布时间】:2013-07-17 04:13:39
【问题描述】:

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

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];
}

【问题讨论】:

  • 你确定你的图片是png而不是jpeg?
  • 是的,图片是 png。
  • 在这一行 NSString *anImagePath = [NSString stringWithFormat:@"%@/%@", aDocumentsDirectory, anImageName]; NSLog(@"路径是=%@",anImagePath);您使用 aDocumentsDirectory 而不是 dataPath,因此图像不会存储在文件夹中

标签: ios nsdocumentdirectory


【解决方案1】:

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

NSString *anImagePath = [NSString stringWithFormat:@"%@/%@", dataPath, anImageName];

【讨论】:

  • 哦,来吧,伙计们!!!他正在使用 stringByAppendingPathComponent: 方法,并且该方法本身可以完成这项工作(在组件之前放置“/”)。
【解决方案2】:

试试这个..

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-05
    • 1970-01-01
    • 2018-11-10
    • 1970-01-01
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    相关资源
    最近更新 更多