我正在实施用户个人资料模块。为此,我想存储用户配置文件,用户从相机或图库中选择。我想将它传递给API,但我不知道该怎么做。
以下是我的代码..
var request = URLRequest(url: URL(string: "http://xxxx.xx/UpdateEmp_test?xx=\(xx)&PermanentAddress=\(ADDrEss)&PermVillage=\(townCITY)&PermTaluk=\(taulkcode!)&PermDist=\(districtcode!)&PermPinCode=\(PINcode)&PermState=\(statecode!)&FatherName=\(FatherNaME)&MotherName=\(MothERName)&profile_image=\(imagePath)")!)
print(request)
request.httpMethod = "POST"
let session = URLSession(configuration: .default)
session.dataTask(with: request) {data, response, error in
guard error == nil && data != nil else
{
let alert = UIAlertController(title: "Check your Internet Connection", message: "", preferredStyle: .alert)
self.present(alert, animated: true, completion: nil)
let when = DispatchTime.now() + 3
DispatchQueue.main.asyncAfter(deadline: when)
{
// your code with delay
alert.dismiss(animated: true, completion: nil)
}
return
}
do
{
let json = try JSONSerialization.jsonObject(with: data!, options: [])
print(json)
let message = (json as AnyObject).value(forKey: "message") as! NSString
print(message)
DispatchQueue.main.sync {
if message == "success" {
let alert = CustomAlert(title:"Request Updated Successfully!")
alert.show(animated: true)
self.dismiss(animated: true, completion:nil)
}
else if message == "Already"{
let alert = CustomAlert(title:"Already Request Updated")
alert.show(animated: true)
}
else {
let alert = CustomAlert(title:"Failed,Try Again!")
alert.show(animated: true)
}
}
}
catch
{
}
}.resume()
答案 0 :(得分:0)
您可以将图像转换为Base64字符串并将其发送到服务器:
let imageData:NSData = UIImagePNGRepresentation(yourUIImage.image)!
编码:
let strBase64 = imageData.base64EncodedString(options: .lineLength64Characters)
解码:
let dataDecoded:NSData = NSData(base64EncodedString: strBase64, options: NSDataBase64DecodingOptions(rawValue: 0))!
let decodedimage:UIImage = UIImage(data: dataDecoded)!
yourImageView.image = decodedimage