I have function uploading image using Alamofire. When I upload it, the image is corrupted. But when I'm save it to device, the image can be opened.
Here's the upload code
Alamofire.upload(multipartFormData: { (_multipartFormData:MultipartFormData) in
for (key, value) in parameters! {
if key == "foto_id" {
_multipartFormData.append(
"\(value)".data(using: String.Encoding.utf8)!,
withName: key,
fileName: "_foto_id.png",
mimeType: "image/png"
)
} else if key == "foto_selfie" {
_multipartFormData.append(
"\(value)".data(using: String.Encoding.utf8)!,
withName: key,
fileName: "_foto_selfie.png",
mimeType: "image/png"
)
} else {
//Data other than image
_multipartFormData.append((value as! String).data(using: .utf8)!, withName: key)
}
}
}, usingThreshold: 1, to: BASE_URL, method: .post) { (encodingResult:SessionManager.MultipartFormDataEncodingResult) in
switch encodingResult {
case .success(let upload, _, _):
upload.responseObject{(response: DataResponse<clsResponsePostFotoID>) in
let reqLoginResponse = response.result.value
completionHandler(reqLoginResponse, nil)
}
break
case .failure(let encodingError):
print(encodingError)
// completionHandler(nil,encodingError as NSError?)
break
}
}
}
If I change "\(value)".data(using: String.Encoding.utf8)! into value as! Data, it'll show error
Cannot invoke 'append' with an argument list of type '(Data?, withName: String, fileName: String, mimeType: String)'
Overloads for 'append' exist with these partially matching parameter lists: (Data, withName: String, fileName: String, mimeType: String), (URL, withName: String, fileName: String, mimeType: String)
How can I fix it? Thank you
"\(value)".data(using: String.Encoding.utf8)!,, No. You are usingdescription, clearly not recommended. What's the class ofvalue?` Data?NSData?UIImage`? - Larme