2017-06-05 166 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