ALAMOFIRE多部分图像的响应图像数据

时间:2017-02-24 19:44:51

标签: iphone swift swift3 alamofire multipartform-data

我编写了一个在服务器上传两个多部分图像的功能,服务器合并这两个图像并返回1个整个图像作为响应。

  

这是功能

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

))

请帮助我获得正确的回复,因为目前它没有成功。

1 个答案:

答案 0 :(得分:0)

它似乎正在上传大量数据,因此您的请求已超时。您可以为您增加超时间隔及其工作。

以下是代码:

let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.timeoutIntervalForResource = 10800 // seconds
configuration.timeoutIntervalForRequest = 10800 // seconds

alamoFireManager = Alamofire.Manager(configuration: configuration)

快乐编码:)