我正在尝试使用 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
}
}
)
}