iPhone应用程序文档目录更改

发布于 2024-09-03 00:16:36 字数 499 浏览 2 评论 0原文

我正在构建一个应用程序,允许用户选择或拍摄照片,然后将其作为 jpeg 保存到应用程序的文档目录中。一切都很完美,但是,我意识到每次在设备上构建并运行 Xcode 项目时,都会重新安装应用程序,从而创建一个新的文档目录路径。有没有办法在每次安装应用程序时保留路径?

这是我用来生成文档目录路径以进行保存的代码:

NSArray *thePathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *finalPath = [[thePathArray objectAtIndex:0] stringByAppendingPathComponent:@"user_image.jpg"];

是否有更好的方法来执行此操作?我可以使用相对路径吗?或者是否没有真正的解决方案,因为每次通过 Xcode 构建时都会重新安装整个应用程序?

谢谢, 豪伊

I'm building an app that allows a user to select or take a photo then it is saved as a jpeg to the app's documents directory. Everything works perfectly, however, I realized that each time I build and run the Xcode project on the device, in reinstalls the app thus creating a new documents directory path. Is there any way to retain the path each time the app is installed?

This is the code I'm using to generate the path to the document's directory for saving:

NSArray *thePathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *finalPath = [[thePathArray objectAtIndex:0] stringByAppendingPathComponent:@"user_image.jpg"];

Is there a better way to do this? Can I use a relative path? Or is there no real solution since the entire app is reinstalled each time it's build via Xcode?

Thanks,
Howie

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隱形的亼 2024-09-10 00:16:36

XCode 确实会重新安装应用程序,但是文档目录、库和首选项都会被保存。它的工作原理与 App Store 更新应用程序的方式类似。

我认为你的问题可能是你每次都用相同的名称保存文件。iPhone文件管理器不会为你附加数字,它只是覆盖现有文件,你必须更改名称你自己。

我建议在末尾附加一个数字,并将当前的数字保存在 NSUserDefaults 中。

顺便说一句,除非您希望用户能够看到您保存到文档目录的文件,否则我会将您的文件保存到库中。目前,iPad 允许您通过 iTunes 查看文档目录,并且该功能将在 iPhone/iPod touch 4.0 中提供。

XCode does reinstall the app, but, the documents directory, library, and preferences are saved. It works similarly to the way the App Store updates your apps.

I think your problem might be that you are saving the file with the same name every time. The iPhone file manager does not append numbers for you, it just overwrites the existing file, you have to change the name yourself.

I would recommend appending a number to the end, and saving the current number you are on in NSUserDefaults.

On a side note, unless you want the user to be able to see the files you are saving to the documents directory, I would save your files to the Library. Currently, the iPad allows you to see the documents directory though iTunes and that functionality will be coming to iPhone/iPod touch with 4.0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文