在服务器中加载imagepicker图像,我尝试了一些正在运行的代码,我选择了要发送到Api的图像,图像参数是File类型,因此如何在File类型中传递图像。
如何将其成像为文件格式?我尝试了很多方法,但没有解决方法。
像这样的api呼叫
kycupload(title1:String, first_name:String,last_name:String,middel_name:String,gender:String, idprofdesc:String,idnumber:String, addprofdesc:String,address:String, address2:String,city:String, state:String,pincode:String,nationality:String,idproofimage:String, addressproofimage:String,pan_no:String)
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
var urlString = " "
if let imgUrl = info[UIImagePickerControllerImageURL] as? URL{
let imgName = imgUrl.lastPathComponent
let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
let localPath = documentDirectory?.appending(imgName)
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
let data = UIImagePNGRepresentation(image)! as NSData
data.write(toFile: localPath!, atomically: true)
//let imageData = NSData(contentsOfFile: localPath!)!
let photoURL = URL.init(fileURLWithPath: localPath!)//NSURL(fileURLWithPath: localPath!)
urlString = String(describing: photoURL)
idproofimage = urlString
}
let param: Parameters = [
"usrtoken":"9V4ymEO6GgjIqQUHFps8fGvmJbX7Abcu-bdf3fd65c81469f9b74cedd497f2f9cehHKHPiR2h",
"title": "Mr",
"firstName": "naga",
"lastName": "take",
"middleName": "",
"gender": "male",
"idprofdesc": "aadhaar",
"idNumber": "393435724657",
"addprofdesc": "passport",
"address": "address",
"address2": "address",
"city": "cuz",
"state": "gagalkj",
"nationality": "agalkbaflg",
"pincode": "123112",
"pan_no": "12312312",
"Idproofimage":urlString,
"Addressproofimage":""]
Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(imageData!, withName: "file", fileName: "swift_file.png", mimeType: "image/png")
for (key, value) in param {
multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key)
}
}, to: "http://3.130.85.122/api/v1/user/sxp/account/kyc-upload")
{ (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print("uploading \(progress)")
})
upload.responseJSON { response in
print("the success full daa",response.result)
}
case .failure( _): break
print("error message")
}
}
mainimage = pickedImage
let cell = Tableview.cellForRow(at: path) as! CustomTableviewCell
cell.documentImage?.image = nil
Tableview.reloadData()
picker.dismiss(animated: true, completion: nil)
}
当我尝试上面的代码时,会得到这种结果:
响应状态:400
{
错误= {
idproofimage =(
“ ID证明图像必须是文件。”
);
;
};
}
如何将图像传递给我的API?