POST图片Swift 3使用Alamofire

时间:2017-01-17 04:11:24

标签: swift swift3 alamofire alamofireimage

我正在尝试使用Alamofire在Swift 3中发布图像。我不确定如何发送我的参数中指定的图像。我想我需要将照片保存到POST,对吗?有没有办法不用先保存照片?

 func postPhoto (image: UIImage) {


        let url = try! URLRequest(url: URL(string:"http://example.com/doapost")!, method: .post, headers: nil)

        Alamofire.upload(
            multipartFormData: { multipartFormData in
                multipartFormData.append(Data(), withName: "image", fileName: "file.png", mimeType: "image/png")
            },
                with: url,
                encodingCompletion: { encodingResult in
                    switch encodingResult {
                    case .success(let upload, _, _):
                        upload.responseJSON { response in
                            if((response.result.value) != nil) {
                                print(response)
                            } else {
                                print("nil response")
                            }
                        }
                    case .failure( _):
                        print("Failure to post")
                        break
                    }
            }
        )
    }

0 个答案:

没有答案