【问题标题】:Having issue uploading image using Alamofire and Swift 4使用 Alamofire 和 Swift 4 上传图片时遇到问题
【发布时间】:2018-07-31 20:23:42
【问题描述】:

我正在尝试通过我的应用将图片从画廊上传到服务器。这是我的代码:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    if let userID:String = userDefaults.string(forKey: "userID"){

        let URL: String = "HERE_IS_URL"
        let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage

        let imageData = UIImageJPEGRepresentation(chosenImage, 1.0)
        dismiss(animated: true, completion: nil)

        userProfilePicture.image = chosenImage
        userProfilePicture.contentMode = .scaleToFill


        let head: HTTPHeaders = [
            "Content-type": "multipart/form-data",
            "key": "key"
        ]

        self.alamoManager?.upload(multipartFormData: { (multipartFormData) in

            if let data = imageData{
                multipartFormData.append(data, withName: "image", fileName: "image.png", mimeType: "image/png")
            }

        }, usingThreshold: UInt64.init(), to: URL, method: .get, headers: head) { (result) in
            switch result{
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    print("response: \(response)")
                    if let err = response.error{
                       print(err)
                        return
                    }
                }
            case .failure(let error):
                print("Error in upload: \(error.localizedDescription)")

            }
        }

     }

}

从图库中选择图像后,我在 imageview 中设置图像。 Api 响应是这样的:

response: FAILURE: Error Domain=NSURLErrorDomain Code=-1001 “请求超时。” UserInfo={NSUnderlyingError=0x608000245760 {错误域=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=MY_URL, NSErrorFailingURLKey=MY_URL, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=- , NSLocalizedDescription=请求超时。}

我正在使用 Swift 4、Xcode 9。请让我知道我做错了什么?

【问题讨论】:

  • 据我所知,您需要多部分中的标识符/访问参数。
  • 关于 multipart 中的标识符/访问参数的任何详细信息? @dahiya_boy
  • 在文件中,你的 api 中必须有一些参数。
  • 使用POST方法发送multipart/form-data。
  • 没用。上传失败 。但没有得到“请求超时”错误。只是上传失败的api响应。 @洛基

标签: ios swift alamofire


【解决方案1】:

这是我使用alamofire(swift 4) 上传图片的工作代码。

  func uploadImage(userImage : UIImage?,withCompletionHandler:@escaping (_ result: Any) -> Void){

    Alamofire.upload(
        multipartFormData: { MultipartFormData in
            if((userImage) != nil){
                MultipartFormData.append(UIImageJPEGRepresentation(userImage!,  0.025)!, withName: "your_tag", fileName: "imageNew.jpeg", mimeType: "image/jpeg")
            }

    }, to: "your_url_here") { (result) in

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

            upload.responseJSON { response in
               // getting success
            }

        case .failure(let encodingError): break
            // getting error
        }


    }
}

A.F.A.I.K. 应该有三种可能。

1) 图片类型可能不匹配(例如,后端应该可以接受扩展名)。

2) 图像大小很重要。

3) 您需要使用正确的 key_tag 发送图像。

【讨论】:

  • 工作。谢谢。但你能告诉我如何添加加载指示器吗? @Rishi P.
  • @EARashel,请参考this link
猜你喜欢
  • 2017-04-22
  • 2019-03-16
  • 2021-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-06
相关资源
最近更新 更多