ios 获取临时目录中文件的文件路径和 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7078808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get file path and URL for file in temp directory
提问by HasgardLucian
I try to get the file path of a file called "temp.pdf" wich is located in the NSTemporaryDirectory folder (I've checked, the file exists).
我尝试获取位于 NSTemporaryDirectory 文件夹中的名为“temp.pdf”的文件的文件路径(我已经检查过,该文件存在)。
I use this :
我用这个:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"temp" ofType:@"pdf" inDirectory:NSTemporaryDirectory()];
I've tried with :
我试过:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"temp.pdf" ofType:@"" inDirectory:NSTemporaryDirectory()];
And :
和 :
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"temp" ofType:@"pdf"];
But it seems that it doesn't work, return value is always null.
但是好像不行,返回值总是空的。
So, i'm a little confused, someone can help me ?
所以,我有点困惑,有人可以帮助我吗?
Thank you.
谢谢你。
回答by Joe
NSTemporaryDirectory()
provides a full path to the temporary directory. Instead of using your mainBundle
have you tried
NSTemporaryDirectory()
提供临时目录的完整路径。而不是使用你mainBundle
有你试过
NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"temp.pdf"];
If you need a URL instead, do this:
如果您需要一个 URL,请执行以下操作:
NSURL *furl = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"temp.pdf"]];
回答by twtr
Swift 2.0
斯威夫特 2.0
let tmpDirURL = NSURL.fileURLWithPath(NSTemporaryDirectory(),isDirectory: true)
let fileURL = tmpDirURL.URLByAppendingPathComponent("stuff").URLByAppendingPathExtension("gif")
print("FilePath: \(fileURL.path)")
回答by bompf
For Swift 3.0
对于 Swift 3.0
var fileUrl: URL = URL(fileURLWithPath: NSTemporaryDirectory())
fileUrl.appendPathComponent("foo")
fileUrl.appendPathExtension("bar")