我正在实现用户配置文件模块。为此,我想存储用户配置文件,该用户配置文件是从相机或画廊中选择的。我想将其传递到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()
您可以将图像转换为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