It may seem like a stupid question, but I have a problem with UIImage.
My app communicates with my Django server, and for that, I'm using Alamofire. I need to pass an image, but I don't know how to convert UIImage to appropriate data so that the server can understand.
My code currently looks like this:
let param = ["image": image] // <- 'image' is a UIImage
AF.request(method: .post, parameters: param) // and so on
The server says the form is invalid. For my server, I'm using a form looks like this:
class UploadImageForScanForm(forms.Form):
image = forms.ImageField()
How can I convert a UIImage to an appropriate data format so that my Django server can understand it and do its work with it?
CodePudding user response:
Since this is a "form", I'd imagine that you are supposed to send a multipart form with the image in it using the POST method, rather than a GET request.
let image = // get your UIImage in some way
guard let png = image.pngData() else {
// there is no PNG representation for the UIImage
// you can also try jpegData
return
}
AF.upload(multipartFormData: { form in
form.append(png, withName: "image", fileName: "image.png", mimeType: "image/png")
}, to: someURL)
// and then you can get the response in whatever way you like
.responseFoo { response in
...
}