如何在 iPhone 上获得可写路径? [英] How can I get a writable path on the iPhone?

查看:62
本文介绍了如何在 iPhone 上获得可写路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发布这个问题是因为我有一个完整的答案写在另一篇文章中,当时我发现它不适用于原版,但我认为它太有用了,不能浪费.因此,我也把它变成了一个社区维基,以便其他人可以充实问题和答案.如果您觉得答案有用,请为这个问题投票 - 作为社区维基,我不应该为此投票获得积分,但它会帮助其他人找到它

如何获取允许在 iPhone 上写入文件的路径?您可以(误导性地)在模拟器上随意书写,但在 iPhone 上,您只能写入特定位置.

How can I get a path into which file writes are allowed on the iPhone? You can (misleadingly) write anywhere you like on the Simulator, but on the iPhone you are only allowed to write into specific locations.

推荐答案

需要考虑三种可写路径 - 第一种是 Documents,您可以在其中存储想要保留的内容并通过 iTunes 提供给用户(如3.2):

There are three kinds of writable paths to consider - the first is Documents, where you store things you want to keep and make available to the user through iTunes (as of 3.2):

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

其次,与 Documents 目录非常相似,有 Library 文件夹,您可以在其中存储配置文件和您也想保留的可写数据库,但您不希望用户能够通过iTunes:

Secondly, and very similar to the Documents directory, there is the Library folder, where you store configuration files and writable databases that you also want to keep around, but you don't want the user to be able to mess with through iTunes:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryDirectory = [paths objectAtIndex:0];

请注意,即使用户使用 3.2 之前的设备(iPad)无法查看 iTunes 中的文件,但 NSLibraryDirectory 常量自 iPhoneOS 2.0 起就可用,因此可用于针对 3.0 的构建(或者甚至更早,如果您仍在这样做).此外,除非您将应用标记为允许用户修改文档,否则用户将无法看到任何内容,因此如果您今天使用 Documents,只要您在更新以支持用户文档时更改位置就可以了.

Note that even though the user cannot see files in iTunes using a device older than 3.2 (the iPad), the NSLibraryDirectory constant has been available since iPhoneOS 2.0, and so can be used for builds targeting 3.0 (or even earlier if you are still doing that). Also the user will not be able to see anything unless you flag an app as allowing users to modify documents, so if you are using Documents today you are fine as long as you change location when updating for support of user documents.

最后有一个缓存目录,您可以在其中放置您不关心的图像是否长期存在(手机可能会在某些时候删除它们):

Last there is a cache directory, where you can put images that you don't care exist for the long term or not (the phone may delete them at some point):

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [paths objectAtIndex:0];
BOOL isDir = NO;
NSError *error;
if (! [[NSFileManager defaultManager] fileExistsAtPath:cachePath isDirectory:&isDir] && isDir == NO) {
    [[NSFileManager defaultManager] createDirectoryAtPath:cachePath withIntermediateDirectories:NO attributes:nil error:&error];
}

请注意,您必须在那里实际创建 Caches 目录,因此在编写时您必须每次都检查和创建!有点痛,但就是这样.

Note that you have to actually create the Caches directory there, so when writing you have to check and create every time! Kind of a pain, but that's how it is.

然后当你有一个可写的路径时,你只需像这样附加一个文件名:

Then when you have a writable path, you just append a file name onto it like so:

NSString *filePath =  [documentsDirectory stringByAppendingPathComponent:@"SomeDirectory/SomeFile.txt"];

NSString *filePath =  [cachePath stringByAppendingPathComponent:@"SomeTmpFile.png"];

使用该路径进行读取或写入.

Use that path for reading or writing.

请注意,您可以在上述示例字符串中使用的任一可写路径中创建子目录(假设已创建).

Note that you can make subdirectories in either of those writable paths, which one of the example string above is using (assuming one has been created).

如果您尝试将图像写入照片库,则不能使用文件系统调用来执行此操作 - 相反,您必须在内存中有一个 UIImage,并使用 UIImageWriteToSavedPhotosAlbum()UIKit 定义的函数调用.您无法控制目标格式或压缩级别,也不能以这种方式附加任何 EXIF.

If you are trying to write an image into the photo library, you cannot use file system calls to do this - instead, you have to have a UIImage in memory, and use the UIImageWriteToSavedPhotosAlbum() function call defined by UIKit. You have no control over the destination format or compression levels, and cannot attach any EXIF in this way.

这篇关于如何在 iPhone 上获得可写路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆