我试图将带有alamofire的tableview内的图像上传到我的服务器。我使用php作为我的后端,已经完成了php部分。问题是我不知道如何在我的alamofire代码中实现图像。任何帮助将不胜感激。
//Submit Button///////////////////////
@IBAction func submit(_ sender: UIButton) {
let cell = sender.superview?.superview as! RegisterCell
var variable01 = cell.variable01.text!
var variable02 = cell.variable02.text!
let registerUrl = "http://www.website.com/register.php?var01=\(variable01)&var02=\(variable02)"
let urlEncodedString = registerUrl.replacingOccurrences(of: " ", with: "%20")
self.parseJSON(url: urlEncodedString)
}
//Parse JSON//////////////////////////
func parseJSON(url : String){
Alamofire.request(url).responseJSON(completionHandler: {
response in
self.parseData(JSONData: response.data!)
})}
func parseData(JSONData : Data){
do {
var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONObject
if let signJSON = readableJSON["Register"] as? [JSONObject] {
for i in 0..<signJSON.count {
let JSON = signJSON[i]
let parseStatus = JSON["sendStatus"] as! String
let parseMessage = JSON["sendMessage"] as! String
if parseStatus == "Error" {print("Error"); self.Alert("\(parseMessage)") }
else if parseStatus == "Success" {
performSegue(withIdentifier: "Register01=>Register02", sender: self)}
}}}
catch {print(error)}
}
答案 0 :(得分:0)
请试试这个
func commonNetworkCallToUploadImage( imageData : Data,withName : String,fileName : String,header :[String:String],url:String,method:HTTPMethod,parameters : [String:Any]?,completionHandler:@escaping (JSON?,String?)->Void) {
Alamofire.upload(multipartFormData: { (formData) in
formData.append(imageData, withName: withName, fileName: fileName, mimeType: "image/jpg")
for (key,value) in parameters!{
let components = String(describing: value)
let data = (components).data(using:.utf8)
formData.append(data!, withName: key)
}
},to: url ,method:method,
headers:header,
encodingCompletion:{ (encodingResult) in
switch encodingResult {
case .success(let upload,_ , _):
upload.responseJSON { response in
if let data = response.result.value {
let json = JSON(data)
completionHandler(json,nil)
}
}
case .failure(let encodingError):
completionHandler(nil,encodingError.localizedDescription)
}
})
}