2017-06-05 137 views
1

有没有什么办法可以将文件保存到文件目录中并使用相对路径?用相对路径将文件保存在文档目录中?

我已经试过这样的事情,但它不工作

UIImage *image = [UIImage imageNamed:@"22.png"]; 
[UIImagePNGRepresentation(image) writeToFile:@"../../Documents/22.png" atomically:true]; 
+0

检索的URL的文件夹试试这个:$ {SRCROOT} /文档/ 22 .png – KKRocks

+0

@KKRocks没有它的不工作 –

回答

1

你需要从NSFileManager

NSString *fileName = @"22.png" 
UIImage *image = [UIImage imageNamed:fileName]; 
NSURL *documentsURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory 
                  inDomain:NSUserDomainMask 
                appropriateForURL:nil 
                   create:NO 
                   error:nil]; 
NSURL *fileURL = [documentsURL URLByAppendingPathComponent:fileName]; 
[UIImagePNGRepresentation(image) writeToURL:fileURL atomically:true]; 
+0

我知道这个功能..但我想以相对路径的方式获取文档目录...它会改变运行时 –

+4

在一个标准的应用程序(不是越狱),这是不可能的。由于URL可能会发生变化,因此每次都必须使用'NSFileManager'(或过期的'NSSearchPaths ...')方法。 – vadian

相关问题