简体   繁体   English

从imagePicker获取本地文件路径(iOS,目标C)

[英]Getting Local File Path from imagePicker (iOS, Objective C)

I am struggling to get the file path from imagePicker and sending it to the server. 我正在努力从imagePicker获取文件路径并将其发送到服务器。

This is the attempt to pass over the local path client-side: 这是尝试通过客户端的本地路径:

NSData *webData = UIImagePNGRepresentation(info[UIImagePickerControllerOriginalImage]);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:@"test.png"];
[webData writeToFile:localFilePath atomically:YES];

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager POST:url.absoluteString
   parameters:@{@"path": localFilePath}
     progress:nil
      success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
... //results always in error

This is the backend code snipped receiving the path key (in JavaScript): 这是截断的接收后端密钥的后端代码(在JavaScript中):

var fs = require('file-system');

file {
 data: fs.readFileSync(path),
}

However, I always get errors like this: 但是,我总是会收到这样的错误:

Error: ENOENT: no such file or directory, open '/Library/Developer/CoreSimulator/Devices... 错误:ENOENT:没有此类文件或目录,请打开'/ Library / Developer / CoreSimulator / Devices ...

Is there a problem in my JS-Code? 我的JS代码有问题吗? Or what other approach should I use to get the file path? 或者我应该使用其他什么方法来获取文件路径? I'd be happy if someone could help me out here. 如果有人可以在这里帮助我,我会很高兴。

PS other popular existing StackOverflow answers on getting the file path induce the same error code. PS其他流行的现有StackOverflow答案在获取文件路径时也会产生相同的错误代码。

It is not possible. 这不可能。 You need to create a Data of that image and upload it using multipart to server. 您需要创建该图像的数据,然后使用多部分上传到服务器。 I hope this link would work for you: 我希望此链接对您有用:

How to get UIImage from local url and post the image to server ? 如何从本地URL获取UIImage并将图像发布到服务器?

Either I've misunderstood the question, or you're trying to do the impossible. 我误解了这个问题,或者您正在尝试解决不可能的事情。

The path on your device (in this case the simulator) isn't accessible to the server, because it only exists on the device. 服务器无法访问您设备上的路径(在本例中为模拟器),因为该路径仅存在于设备上。

If you want to pass the actual image across, you need to send the data itself in the request, you can't have the server get it's path and reach over the internet into the device to get it. 如果要传递实际的图像,则需要在请求中发送数据本身,您无法让服务器获取其路径并通过Internet到达设备以获取它。

The accepted answer to this question is an example of how to attach the image's data to an AFNetworking request. 问题的公认答案是如何将图像数据附加到AFNetworking请求的示例。 Unfortunately, I don't know enough about the server to help much there but I'd expect you to be able to get it the same way you got your path out of the request, it would just the the raw image data instead of a file path. 不幸的是,我对服务器的了解不足,无法为您提供很多帮助,但是我希望您能够像从请求中获取路径一样获得它,它只是原始图像数据而不是原始图像数据。文件路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM