【问题标题】:ALAMOFIRE response image data for multipart image多部分图像的 ALAMOFIRE 响应图像数据
【发布时间】: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


【解决方案1】:

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

代码如下:

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

alamoFireManager = Alamofire.Manager(configuration: configuration)

快乐编码:)

【讨论】:

  • 我尝试了代码但同样的问题,let manager = Alamofire.SessionManager.default manager.session.configuration.timeoutIntervalForRequest = 10800 manager.session.configuration.timeoutIntervalForResource = 10800 manager.upload(multipartFormData:{
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 2015-10-15
  • 2019-11-22
  • 2018-08-26
  • 1970-01-01
相关资源
最近更新 更多