Vytvářím aplikaci, kde uživatel může aktualizovat uživatelský profil. Ale co když uživatel nechce, aby se změnit obrázek, a to pouze změny v první jméno nebo příjmení , Ale ne změnit obrázek profilu. může Prosím nějaký průvodce mě , jsem pomocí Alamofire a awss3 pro upload obrázku , Tady je můj kód
func saveProfile(imagename: String )
{
if self.firstNameTxt.text!.isEmpty || self.lastNameTxt.text!.isEmpty || self.birthDateTxt.text!.isEmpty {
let alertController = UIAlertController(title: "Error", message: "Please Fill All THE FIELDS", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
present(alertController, animated: true, completion: nil)
} else {
let parameter : Parameters = [
"first_name": self.firstNameTxt.text!,
"last_name": self.lastNameTxt.text!,
"birth_date" : self.birthDateTxt.text!,
"profile_pic" : imagename
]
SVProgressHUD.show()
MVCServer().serviceRequestWithURL(reqMethod: .patch, withUrl: "user-profile", withParam: parameter, diplayHud: true, includeToken: true) { responseCode, response in
if responseCode == 1 {
if let dataResponse = response.value(forKey: "result") as? Bool , let payload = response.value(forKey: "payload") as? NSDictionary{
if dataResponse == true {
if let userData = LoginModel.init(dictionary: payload) {
Utility.setModelObject(userData , forKey: ProfileData)
self.view.makeToast("Profile Updated Succefully", duration: 3.0, position: .top)
}
}
}
} else {
Utility.showMessage("Error", withTitle: "Error While Uploading Data", on: self)
print(response.value(forKey: "message") as? String ?? "")
SVProgressHUD.dismiss()
}
}
}
}
tohle je moje tlačítko uložit akce
@IBAction func saveButtonTapped(_ sender: UIButton) {
uploadImage()
}
A Tady volám Nahrávání Obrazu Api.
func uploadImage()
{
if let profileImageData = profileImageData
{
let imageName:Int64 = Int64(Date().timeIntervalSince1970)
let randomString = Utility.getAlphaNumericRandomNumber()
let imageNameStr = String(format:"%@%d.jpg",randomString,imageName)
let stringUploadPath = String(format:"%@/%@",AmazonAWS3.S3UploadPathProfile,imageNameStr)
Utility.showLoadingView()
AmazonAWS3().uploadFileToAWS3(filePath: stringUploadPath, data: profileImageData) { result in
switch result {
case .success(_):
self.saveProfile(imagename: imageNameStr)
case .failure(_):
print("Error While Uploading Image")
}
}
}
}