gpt4 book ai didi

ios - 将文件从主包复制到文档目录(沙箱)

转载 作者:行者123 更新时间:2023-11-28 18:36:40 41 4
gpt4 key购买 nike

我需要在第一次运行我的应用程序时将一些 png 从主包复制到文档文件夹。我需要它,因为我必须将这些图像的文件路径保存在核心数据实体中。

我试过了,但是不行,有人知道为什么吗?

// to copy image from main bundle to documents folder

NSString* mainBundleFilePath = [[NSBundle mainBundle] pathForResource:@"SbriciolataDiRicotta" ofType:@"png"];

NSArray *destPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [destPath objectAtIndex:0];

NSData *mainBundleFile = [NSData dataWithContentsOfFile:mainBundleFilePath];
[[NSFileManager defaultManager] createFileAtPath:documentsDirectory
contents:mainBundleFile
attributes:nil];

// to save the file path

NSString*nomeImmagine = [[NSString alloc] initWithFormat:@"SbriciolataDiRicotta"];
NSString *pngFilePath = [NSString stringWithFormat:@"%@/%@.png",documentsDirectory, nomeImmagine];

谢谢

最佳答案

createFileAtPath 期望将要创建的文件 的路径作为参数,而不是路径封闭的目录

备注:

  • 您也可以使用 copyItemAtPath:toPath:error: 来复制文件而不加载全部内容存入内存。
  • 不需要使用 initWithFormat 来分配常量字符串。
  • stringByAppendingPathComponent: 是附加路径组件的正确方法。

关于ios - 将文件从主包复制到文档目录(沙箱),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17542403/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com