我是iOS的新手,使用Alamofire上传图片。这是我的代码:
let image = imageView.image
let parameters = ["profile_picture" : "pic"]
let fileData = UIImageJPEGRepresentation(image!, 0.2)
let URL2 = try! URLRequest(url: "######", method: .post, headers: ["Authorization" : "######"])
configuration.timeoutIntervalForRequest = 200
manager = Alamofire.SessionManager(configuration: configuration)
manager.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(fileData!, withName: "profile_picture", fileName: "upfile", mimeType: "image/jpg")
for (key, value) in parameters {
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
}
}, with: URL2 , encodingCompletion: { (result) in
switch result {
case .success(let upload, _, _):
upload.responseJSON {
response in
debugPrint(response)
if let JSON = response.result.value as? [String : Any]{
let messageString = JSON["message"] as? String
debugPrint(messageString)
}else {
let alertError = UIAlertController(title: "Alert", message: "Image upload error", preferredStyle: UIAlertControllerStyle.alert)
alertError.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
self.present(alertError, animated: true, completion: nil)
}
}
case .failure(let encodingError):
print(encodingError)
let alertError = UIAlertController(title: "Alert", message: "Image upload error", preferredStyle: UIAlertControllerStyle.alert)
alertError.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
self.present(alertError, animated: true, completion: nil)
}
}
)
当我运行此代码时,出现以下错误:
2017-11-07 12:54:13.055846+0500 TestApiProject[5972:136877] TIC Read Status [1:0x604000177a00]: 1:57
2017-11-07 12:54:13.057150+0500 TestApiProject[5972:136877] Task <BF98A675-2BCE-4248-A045-EA2D35971EAA>.<1> HTTP load failed (error code: -1005 [4:-4])
2017-11-07 12:54:13.058097+0500 TestApiProject[5972:136879] Task <BF98A675-2BCE-4248-A045-EA2D35971EAA>.<1> finished with error - code: -1005
[Request]: POST http://.....
[Response]: nil
[Data]: 0 bytes
[Result]: FAILURE: Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={NSUnderlyingError=0x604000245e50 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={_kCFStreamErrorCodeKey=-4, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=http://staging.discretelogix.com:5002/api/v1/users/profile-picture, NSErrorFailingURLKey=http://staging.discretelogix.com:5002/api/v1/users/profile-picture, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-4, NSLocalizedDescription=The network connection was lost.}
[Timeline]: Timeline: { "Request Start Time": 531734052.362, "Initial Response Time": 531734052.721, "Request Completed Time": 531734053.071, "Serialization Completed Time": 531734053.077, "Latency": 0.359 secs, "Request Duration": 0.710 secs, "Serialization Duration": 0.006 secs, "Total Duration": 0.715 secs }
我搜索了这个错误,我尝试过的事情是:
但不幸的是,以上都没有解决我的问题。
在另一个地方,我读到了&#34; Keep Alive&#34;标头中的参数。并且iOS中Keep alive的默认值是30秒,在我的情况下需要增加。但我发现Alamofire没有任何相关内容。我刚刚发现它属于底层URLSession,需要更新。
所以我的问题是:
我希望我对自己需要的东西足够清楚。我是iOS的新手,从早上起就一直在搜索这个错误。任何帮助将非常感激。
P.s,我正在使用:
Xcode 9,Swift 3.2,Alamofire 4