【发布时间】:2017-02-24 19:44:51
【问题描述】:
我写了一个函数,它在服务器上上传两个多部分图像,服务器合并这两个图像并返回一个完整的图像作为响应。
这是函数
func apiObjectInPainting(image: UIImage, maskedimage: UIImage,completion: @escaping (Bool) -> Swift.Void) {
if let data = UIImageJPEGRepresentation(image,1.0) {
let filename = getDocumentsDirectory().appendingPathComponent("image1.jpg")
try? data.write(to: filename)
}
if let data = UIImageJPEGRepresentation(maskedimage,1.0) {
let filename = getDocumentsDirectory().appendingPathComponent("image2.jpg")
try? data.write(to: filename)
}
let fileURL1 = getDocumentsDirectory().appendingPathComponent("image1.jpg")
let fileURL2 = getDocumentsDirectory().appendingPathComponent("image2.jpg")
NSLog("Files being stored @\(fileURL1)")
KVNProgress.show(withStatus: "Replacing Object")
Alamofire.upload(multipartFormData:{ multipartFormData in
multipartFormData.append(fileURL1, withName: "image")
multipartFormData.append(fileURL2, withName: "image")
multipartFormData.append("SECKEY".data(using: .utf8)!, withName: "Authorization")
},
usingThreshold:UInt64.init(),
to:"[URL]",
method:.post,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.response { response in
debugPrint(response)
KVNProgress.dismiss()
completion(true)
}
case .failure(let encodingError):
KVNProgress.dismiss()
print(encodingError)
}
})
}
在 encodeCompletion 我有
case .success(let upload, _, _):
upload.response { response in
debugPrint(response)
KVNProgress.dismiss()
completion(true)
}
但在我得到的回应中,
Alamofire.DefaultDataResponse(request: Optional([URL]), response: nil, data: Optional(0 bytes), error: Optional(Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={NSUnderlyingError=0x600000446450 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=[URL], NSErrorFailingURLKey=[URL], _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102, NSLocalizedDescription=The request timed out.}), timeline: Timeline: { "Request Start Time": 509658047.051, "Initial Response Time": 509658047.386, "Request Completed Time": 509658166.018, "Serialization Completed Time": 509658166.021, "Latency": 0.335 secs, "Request Duration": 118.967 secs, "Serialization Duration": 0.003 secs, "Total Duration": 118.970 secs }, _metrics: Optional((Task Interval) <_NSConcreteDateInterval: 0x6000004223c0> (Start Date) 2017-02-24 19:40:47 +0000 + (Duration) 118.966891 seconds = (End Date) 2017-02-24 19:42:46 +0000
(Redirect Count) 0
(Transaction Metrics) (Request) <NSURLRequest: 0x600000016020> { URL: [URL] }
(Response) (null)
(Fetch Start) 2017-02-24 19:40:47 +0000
(Domain Lookup Start) 2017-02-24 19:40:47 +0000
(Domain Lookup End) 2017-02-24 19:40:47 +0000
(Connect Start) 2017-02-24 19:40:47 +0000
(Secure Connection Start) (null)
(Secure Connection End) (null)
(Connect End) 2017-02-24 19:40:47 +0000
(Request Start) 2017-02-24 19:40:47 +0000
(Request End) 2017-02-24 19:41:47 +0000
(Response Start) 2017-02-24 19:40:47 +0000
(Response End) (null)
(Protocol Name) http/1.1
(Proxy Connection) NO
(Reused Connection) NO
(Fetch Type) Network Load
))
请帮助我得到正确的响应,因为目前它没有成功。
【问题讨论】:
-
返回的错误是“响应超时”。也许是因为该方法需要很长时间才能完成。你能用一个只返回一些静态响应的简单方法进行测试吗?
标签: iphone swift swift3 alamofire multipartform-data