简体   繁体   English

如何将文件加载到Iphone文档目录?

[英]How to load a file into Iphone documents directory?

I have several large files that I want to load into the applications document folder and then call up from there on demand. 我有几个大文件要加载到应用程序文档文件夹中,然后按需从那里调用。

The suggestion I've seen is to put them in the resources folder and then when the application loads copy them into the documents folder. 我看到的建议是将它们放在资源文件夹中,然后在应用程序加载时将它们复制到documents文件夹中。 I assume that if I do this, these large files will all be loaded into memory when the application starts up and then stay there... 我假设如果这样做,这些大文件将在应用程序启动时全部加载到内存中,然后保留在那里。

Is there some way of having these files go directly into the documents folder when the application is installed, and get loaded into memory only when they are called for? 是否有某种方法可以使这些文件在安装应用程序后直接进入documents文件夹,并仅在需要它们时才加载到内存中?

Thanks... 谢谢...

There's no way to do this automatically at the installation. 无法在安装时自动执行此操作。

You can use NSFileManager like that to copy your files. 您可以像这样使用NSFileManager复制文件。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *finalPath = [documentsDirectory stringByAppendingPathComponent:@"documentName"];
[[NSFileManager defaultManager] copyItemAtPath:yourDocumentPath toPath:finalPath error:nil];

Depends on where do the files originally reside. 取决于文件最初所在的位置。 If in your bundle, then use NSFileManager, like gcamp said. 如果在您的捆绑软件中,则使用NSFileManager,如gcamp所述。 If you download them from the 'Net, or generate them on the fly, use NSOutputStream - that's the API for writing binary or text data from memory into files. 如果您从网上下载它们,或者即时生成它们,请使用NSOutputStream-这是用于将内存中的二进制或文本数据写入文件的API。 Or use POSIX open()/write(), old school. 或使用POSIX open()/ write(),老派。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM