Alamofire错误代码= -1000“URL未指向文件URL” [英] Alamofire error Code=-1000 "The URL does not point to a file URL"

查看:172
本文介绍了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: "http://app.staging.acme.com/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错误代码= -1000“URL未指向文件URL”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆