gpt4 book ai didi

ios - Swift - HTTP 加载失败(通过 alamofire 上传图像时错误代码 : -1005 [4:-4]),。

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:06:38 26 4
gpt4 key购买 nike

我是 iOS 新手,使用 Alamofire 上传图片。我写的代码如下:

let image = imageView.image
let imgData = UIImageJPEGRepresentation(image!, 0.2)!
let headers: HTTPHeaders = [
"x-access-token": "######",
"Accept": "application/json"
]
let parameters = ["profile_picture": "kinza"]
let url = try! URLRequest(url: URL(string:"######")!, method: .post, headers: headers)

Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(imgData, withName: "profile_picture",fileName: "kinza.jpg", mimeType: "image/jpg")
for (key, value) in parameters {
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
},
with: url)
{ (result) in
switch result {
case .success(let upload, _, _):

upload.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})

upload.responseJSON { response in
print(response.result.value)
}

case .failure(let encodingError):
print(encodingError)
}
}

当我运行这段代码时,我得到以下日志:

2017-11-07 11:03:21.595826+0500 TestApiProject[2457:51089] [] nw_socket_get_input_frames recvmsg(fd 6, 4096 bytes): [54] Connection reset by peer
2017-11-07 11:03:21.596094+0500 TestApiProject[2457:51089] TIC Read Status [1:0x6040003612c0]: 1:54
2017-11-07 11:03:21.596495+0500 TestApiProject[2457:51089] [] nw_socket_output_finished shutdown(6, SHUT_WR): [57] Socket is not connected
2017-11-07 11:03:21.597203+0500 TestApiProject[2457:51089] Task <9A25E63E-EC42-419C-A0B7-02998177EDCA>.<1> HTTP load failed (error code: -1005 [4:-4])
2017-11-07 11:03:21.597681+0500 TestApiProject[2457:51091] Task <9A25E63E-EC42-419C-A0B7-02998177EDCA>.<1> finished with error - code: -1005
Upload Progress: 1.0
nil

它已搜索但没有找到任何特定于“错误代码 -1005”的内容 我该如何解决?我错过了什么吗?

我对此有一些疑问:

  • 如果 http 加载失败,那么控制如何转到成功 block Alamofire 请求?
  • response.result.value 的值为 nil。我的有问题吗结束还是在服务器端?
  • 我是否需要向我的 info.plist 添加更多属性?这是怎么回事目前是。

enter image description here

任何帮助将不胜感激。

最佳答案

试试这个..

let image = imageView.image
let imgData = UIImageJPEGRepresentation(image, 0.2)!

let parameters = ["profile_picture": "kinza"]

Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(imgData, withName: "profile_picture",fileName: "kinza.jpg", mimeType: "image/jpg")
for (key, value) in parameters {
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
},to:"http://YOUR URL"){ (result) in

switch result {
case .success(let upload, _, _):

upload.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})

upload.responseJSON { response in
print(response.result.value!)
}

case .failure(let encodingError):
print(encodingError)
}
}

这对我有用..

关于ios - Swift - HTTP 加载失败(通过 alamofire 上传图像时错误代码 : -1005 [4:-4]),。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47151184/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com