我怎样才能得到一个可写的路径在iPhone上? [英] How can I get a writable path on the iPhone?

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

问题描述

我发布这个问题,因为我有一个完整的答案,这个写出来的另一篇文章,当我发现它不适用于原来的,但我认为是太浪费了太有用。因此,我也把这个社区维基做成了一个社区维基,以便其他人可以提出问题和答案。如果你觉得这个答案有用,请投票回答这个问题 - 作为一个社区维基,我不应该得到这个投票的积分,但它会帮助其他人找到它。

>如何获得允许在iPhone上进行文件写入的路径?你可以(误导)在模拟器上任何你喜欢的地方写下来,但是在iPhone上你只能写入到特定的位置。 解决方案

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

  NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); 
NSString * documentsDirectory = [paths objectAtIndex:0];其次,和Documents目录非常相似,还有Library文件夹,您可以在其中存储配置文件和可写的数据库,你也想保持周围,但你不希望用户能够通过iTunes搞砸:

  NSArray *路径= NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,是); 
NSString * libraryDirectory = [paths objectAtIndex:0];请注意,即使用户无法使用超过3.2版本的设备(iPad)在iTunes中查看文件, ,自从iPhoneOS 2.0以来,NSLibraryDirectory常量已经可用,因此可以用于定位3.0的构建(如果仍然这样,甚至更早)。此外,用户将无法看到任何东西,除非您将应用程序标记为允许用户修改文档,所以如果您今天正在使用文档,只要在更新用于支持用户文档的位置时更改位置即可。



最后是一个缓存目录,您可以在其中放置您不关心的图片,并且长时间不存在(手机会在某个时候删除它们):

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

$ / code>

请注意,您必须在其中创建Caches目录,每次都要检查和创建!然后,当你有一个可写的路径时,你只需要添加一个文件名就可以了:

$ / $> b
$ b

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

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

使用该路径读取或写入。

<注意,你可以在这些可写路径中的任何一个上创建子目录,上面的例子中的一个字符串正在使用(假设已经创建了一个)。



如果你是试图写一个图片到图片库,你不能使用文件系统调用来做到这一点 - 相反,你必须在内存中有一个UIImage,并使用 UIImageWriteToSavedPhotosAlbum()函数调用由UIKit定义。您无法控制目标格式或压缩级别,也无法以这种方式附加任何EXIF。


I am posting this question because I had a complete answer for this written out for another post, when I found it did not apply to the original but I thought was too useful to waste. Thus I have also made this a community wiki, so that others may flesh out question and answer(s). If you find the answer useful, please vote up the question - being a community wiki I should not get points for this voting but it will help others find it

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.

解决方案

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

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

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

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

or

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).

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天全站免登陆