I want to send an image from gallery to server in an API call. This image has to be passed as a parameter. To pass the image as a parameter, I tried to get the url of the image like so but it wasn't giving the correct url..
var selectedImage : UIImage = image
let imageData: NSData = UIImagePNGRepresentation(selectedImage)! as NSData
let imageStr = imageData.base64EncodedString(options:.endLineWithCarriageReturn)
imageArray.append(image)
Also I tried to upload the image like so...
for img in imageArray {
let url = "http://myApp..com/a/images_upload"
let headers = [ "Content-Type":"application/x-www-form-urlencoded"]
let URL = try! URLRequest(url: url, method: .post, headers: headers)
let parameters =
[
"access_token": accessToken
"image": img
] as [String : Any]
let imgData = UIImageJPEGRepresentation(img, 0.2)!
Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(imgData, withName: "image",fileName: "file.jpg", mimeType: "file")
for (key, value) in parameters {
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
}
}, with: URL) { (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})
upload.responseJSON { response in
print(response.result.value)
if let value = response.result.value {
print("IMG UPLOADED!!!")
}
}
case .failure(let encodingError):
print(“ERROR”)
}}}
But this is also crashing. I've been having this issue for quite some time now...not able to figure out what the exact solution is...Hope someone can help...:) Also did go through a lot of similar questions on SO. But couldn't find a solution till...
EDIT: My parameters are:
let Parameters =
[
"access_token": commonVarForAccessToken,
"seller_id": idForNewOldUser,
"product_id": self.productId,
"is_default": "1",
"sequence": 1,
"image": self.localPath
] as [String : Any]