如果用户未在 api 调用中上传个人资料图片,如何更新用户数据

How to Update User data if user not uploaded Profile Picture In api call

我正在创建一个应用程序,用户可以在其中更新用户个人资料。但是,如果用户不想更改图像而只更改名字或姓氏,而不更改个人资料图片怎么办。请指导我,我正在使用 Alamofire 和 awss3 上传图片,这是我的代码

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()
            }
        }
        
    }
}

这是我的保存按钮操作

 @IBAction func saveButtonTapped(_ sender: UIButton) {
    
    uploadImage()
   
}

我在这里调用上传图片 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")
            }
        }
    }
}

根据您的实施,最简单的方法是在您的 VC(或 VM 或您需要的地方)中使用字典来存储您的用户已更改的更新值。

class ProfileViewController: UIViewController {

   @IBOutlet var firstNameLabel: UILabel!
   @IBOutlet var lastNameLabel: UILabel!

   var updatedValues: [String : String] = [:] // or [String : Any] 

   // If you are using Storyboard you need to link the IBOutlet to the value changed event
   @IBOutlet func firstName_onChange(_ sender: UILabel) {
      // do some validation 
      updatedValues["first_name"] = sender.text
   }

   // Same outlet for lastName and birthdate

}

对于您的图片,生成 imagename 的方式有点不清楚,但您可以使用相同的方法。当用户从图库中选择图片时,您可以通过添加 profile_pic key/value 来更新 updatedValues

在执行 saveProfile 时,您的 updatedValues 应该只包含用户更改的内容,如果包含 profile_pic 键,您还可以调用 uploadImage