iPhone - 来自本地文件 URL 的 NSData

发布于 2024-08-06 19:12:03 字数 248 浏览 7 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

独享拥抱 2024-08-13 19:12:03
// Given some file path URL: NSURL *pathURL
// Note: [pathURL isFileURL] must return YES
NSString *path = [pathURL path];
NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];

SWIFT代码:

let data = FileManager.default.contents(atPath: path)
// Given some file path URL: NSURL *pathURL
// Note: [pathURL isFileURL] must return YES
NSString *path = [pathURL path];
NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];

Swift code:

let data = FileManager.default.contents(atPath: path)
猫瑾少女 2024-08-13 19:12:03

要完成这项工作,您只需要做:

NSURL *imgPath = [[NSBundle mainBundle] URLForResource:@"search" withExtension:@"png"];
NSString*stringPath = [imgPath absoluteString]; //this is correct  

//you can again use it in NSURL eg if you have async loading images and your mechanism 
//uses only url like mine (but sometimes i need local files to load)
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:stringPath]];
UIImage *ready = [[[UIImage alloc] initWithData:data] autorelease];

To get this work you just need to do:

NSURL *imgPath = [[NSBundle mainBundle] URLForResource:@"search" withExtension:@"png"];
NSString*stringPath = [imgPath absoluteString]; //this is correct  

//you can again use it in NSURL eg if you have async loading images and your mechanism 
//uses only url like mine (but sometimes i need local files to load)
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:stringPath]];
UIImage *ready = [[[UIImage alloc] initWithData:data] autorelease];
皇甫轩 2024-08-13 19:12:03

确保您的本地网址以“file://”开头
那么你只需要这样做:

 NSData *fileData = [NSData dataWithContentsOfFile:fileURL.path];

Make sure your local URL starts with "file://"
then you would just have to do this:

 NSData *fileData = [NSData dataWithContentsOfFile:fileURL.path];
凉城凉梦凉人心 2024-08-13 19:12:03

对于 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
}

↙温凉少女 2024-08-13 19:12:03
guard let data = FileManager.default.contents(atPath: path) else
    { ...
guard let data = FileManager.default.contents(atPath: path) else
    { ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文