iPhone - 来自本地文件 URL 的 NSData
我有一个 NSURL 对象,它为我提供了本地文件的路径(在文档文件夹中)。我想用该文件的内容填充 NSData
对象。尝试使用 dataWithContentsOfURL:
但这失败了。我知道该文件存在,因为 iPhone SDK
返回路径。
有人可以告诉我如何从本地文件的 URL
获取 NSData
对象吗?
谢谢。
I have a NSURL
object which gives me the path of a local file (in documents folder). I want to populate an NSData
object with the contents of this file. Tried using dataWithContentsOfURL:
but this fails. I know the file exists because the iPhone SDK
returns the path.
Can someone please tell me how I can get an NSData
object from the URL
of a local file?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
SWIFT代码:
Swift code:
要完成这项工作,您只需要做:
To get this work you just need to do:
确保您的本地网址以“file://”开头
那么你只需要这样做:
Make sure your local URL starts with "file://"
then you would just have to do this:
对于 swift3,我发现以下 URL 的答案很有帮助
(使用phonegap插件文件捕获图像后,我得到了以下路径
:///var/mobile/Containers/Data/Application/B64B06DE-7976-45CF-9BE2-661F0F309A06/tmp/abcded .jpg )
https://stackoverflow.com/a/41043447/6383908
如果让 videoURL = URL(string: urlString) ,让 videodata = 尝试?数据(内容:videoURL){
//这里添加Alamofire的代码
}
For swift3 I found answer from following URL helpful
( I was getting following path , after capturing image using phonegap plugin
file:///var/mobile/Containers/Data/Application/B64B06DE-7976-45CF-9BE2-661F0F309A06/tmp/abcded.jpg )
https://stackoverflow.com/a/41043447/6383908
if let videoURL = URL(string: urlString), let videodata = try? Data(contentsOf: videoURL) {
//Add code of Alamofire here
}