用alamofire / mulipart发送图片

时间:2018-06-20 07:17:38

标签: swift xcode alamofire multipart

如何发送多张图片?我测试了这段代码,但是服务器说:对象引用未设置为对象的实例。 问题出在哪里?或任何人都可以建议我可以尝试的新代码... 我正在研究三天,我得到了相同的结果。 参数只是这两个ID和文件。 问题出在哪里?

  func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info:[String : Any]) {
    print("control")
    var tempImage:UIImage = info[UIImagePickerControllerOriginalImage] as! UIImage
    tempImage = resizeImage(image: tempImage, newWidth: 300)!

    let data = UIImageJPEGRepresentation(tempImage, 0.7)
    updateProfile(imageData: data)

}

  func updateProfile(imageData:Data?) {
        print(imageData)
        var parameters : [String:String] = [:]
        parameters["id"] = "2011"
        parameters["contactid"] = "2030"
    print(parameters)
        let url = "https://madyar.org/MessageAPI/PostFileMessage"
        print(url)

        Alamofire.upload(multipartFormData: { (multipartFormData) in
            for (key, value) in parameters {
                multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key as String)
            }

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

        }, usingThreshold: UInt64.init(), to: url, method: .post) { (result) in
            switch result{
            case .success(let upload, _, _):
                upload.responseString { response in
                    print("Succesfully uploaded  = \(response)")
                    if let err = response.error{

                        print(err)
                        return
                    }

                }
            case .failure(let error):
                print("Error in upload: \(error.localizedDescription)")

            }
        }

}

1 个答案:

答案 0 :(得分:0)

使用此代码可以正常工作。与此同时,您还可以检查上传进度。

 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {

        uploadImage(image: pickedImage!)

    }
    picker.dismiss(animated: true, completion: nil)
}

func uploadImage(image : UIImage){
    var parameters : [String:String] = [:]
    parameters["id"] = "2011"
    parameters["contactid"] = "2030"
    print(parameters)

    Alamofire.upload(multipartFormData: { (multipartFormData) in
        multipartFormData.append(UIImageJPEGRepresentation(image, 0.2)!, withName: "image_url", fileName: "image.jpeg", mimeType: "image/jpeg")
        for (key, value) in parameters {
            multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
        }
    }, to:"https://madyar.org/MessageAPI/PostFileMessage")
    { (result) in
        switch result {
        case .success(let upload, _, _):

            upload.uploadProgress(closure: { (progress) in
                print(progress)
            })

            upload.responseJSON { response in
                print(response)
            }

        case .failure(let encodingError):
            print(encodingError)
        }
    }
}