Alamofire错误代码=

编程入门 行业动态 更新时间:2024-10-14 06:23:08
本文介绍了Alamofire错误代码= -1000“URL未指向文件URL”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

从我正在开发的iOS应用程序上将图像上传到我的服务器时出现问题。我正在使用Alamofire和 UIImagePickerController 。

Having issues uploading images to my server from the iOS application I'm developing. I'm using Alamofire, and a UIImagePickerController.

在 didFinishPickingMediaWithInfo 委托方法内我保存用户选择的文件 NSURL 来自 info [UIImagePickerControllerReferenceURL] 的变量名为 self.imageNSURL 。

Inside the didFinishPickingMediaWithInfo delegate method I'm saving the file the user selects as a NSURL from info[UIImagePickerControllerReferenceURL] in a variable named self.imageNSURL.

将此传递给Alamofires上传multipartFormData方法(几乎是标准的复制和粘贴,来自他们的 docs )

Passing this along to Alamofires upload multipartFormData method as such (pretty much a standard copy and paste from their docs)

Alamofire.upload( .POST, URLString: "app.staging.acme/api/users/\(id)/picture", multipartFormData: { multipartFormData in multipartFormData.appendBodyPart(fileURL: self.imageNSURL, name: "image") }, encodingCompletion: { encodingResult in switch encodingResult { case .Success(let upload, _, _): upload.responseJSON { request, response, JSON, error in println(JSON) } case .Failure(let encodingError): println(encodingError) } } )

我得到的回报错误是

Error Domain=com.alamofire.error Code=-1000 "The operation couldn’t be completed. The URL does not point to a file URL: assets-library://asset/asset.JPG?id=00000000-0000-0000-0000-000000000000&ext=JPG" UserInfo=0x00000000000 {NSLocalizedFailureReason=The URL does not point to a file URL: assets-library://asset/asset.JPG?id=00000000-0000-0000-0000-000000000000&ext=JPG}

请注意,我在这篇文章的回复中已经确认了ID,实际的错误信息包含有效的信息。

Please note, I've nerfed the ID's in the response for this post, the actual error message contains valid ones.

推荐答案

这是由于从 info [UIImagePickerControllerReferenceURL]返回的URL 这个URL指向assets-library / asset,这是由于沙盒因此,您无法使用此URL访问该文件,这就是为什么alamofire会抱怨您的URL不是文件URL。要解决此问题,您可以使用 multipartFormData.appendBodyPart(data:data,name:name)此方法将数据直接发送为 NSData 。完整代码示例:

Well this is due to the URL returned from info[UIImagePickerControllerReferenceURL] this URL points to assets-library/asset, this due to sandboxing. So you cannot access the file using this URL thats why alamofire complains that your URL is not a file URL. To solve this problem you can use multipartFormData.appendBodyPart(data: data, name: name) this method takes the data to be sent directly as NSData. Full code example:

let imagePicked = info[UIImagePickerControllerOriginalImage] let imageExtenstion = info[UIImagePickerControllerReferenceURL] // imageExtenstion will be "asset.JPG"/"asset.JPEG"/"asset.PNG" // so we have to remove the asset. part var imagePickedData : NSData switch imageExtenstion { case "PNG": imagePickedData = UIImagePNGRepresentation(imagePicked)! // compressionQuality is a float between 0.0 and 1.0 with 0.0 being most compressed with lower quality and 1.0 least compressed with higher quality case "JPG", "JPEG": imagePickedData = UIImageJPEGRepresentation(image, compressionQuality)! } Alamofire.upload(.POST, YOUR_URL, multipartFormData: { multipartFormData in multipartFormData.appendBodyPart(data: imagePickedData, name: imageName) }, encodingCompletion: { encodingResult in switch encodingResult { case .Success(let upload, _, _): upload.responseJSON { request, response, JSON, error in print(JSON) } case .Failure(let encodingError): print(encodingError) } })

更多推荐

Alamofire错误代码=

本文发布于:2023-11-26 02:23:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632272.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误代码   Alamofire

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!