After a very thorough study, I found out that this can be done easily with the help of an additional chain and type casting.
The first step: to separate the parameters by type, pointing it at the string and Image , and check for the String value and the Image value.
let parameters: [String : AnyObject? ] = [ "first_name": "XXXXX", "email" : " 1234@gmail.com ", "password" : "password", "profile_pic" : UIImage(named: "logo")]
Do it in the request method
for (key, value) in parameters { if let value1 = value as? String { multipartFormData.appendBodyPart(data: value1.dataUsingEncoding(NSUTF8StringEncoding)!, name: key) } if let value1 = value as? UIImage { let imageData = UIImageJPEGRepresentation(value1, 1) multipartFormData.appendBodyPart(data: imageData!, name: key, fileName: "logo.png" , mimeType: "image/png") }
You do not need to divide the parameters into two, one for string values ββand the other for the image, and you do not need to convert the image to a string. Hope this solution helps!
source share