首页 > 解决方案 > 当我将它修补到 json 服务器 swift 时,照片没有更新

问题描述

我尝试使用 json 修补服务器上的图像文件,当用户尝试编辑个人资料照片时,我的照片图像来自 ui 选择器视图,它来自库照片,作为响应数据,我们有两个选项: 1 - 响应说:请插入图片和 2 张个人资料照片已更新,我尝试使用以下代码将其发送并在服务器上进行修补,但作为回应,我得到“请插入图片”并且我的个人资料图片在模拟器上更改,但在服务器上不更改这是我的代码:

这是我的编辑按钮代码当我想选择要配置的图像时:

@IBAction func chooseImage(_ sender: Any) {
    
    let actionSheet = UIAlertController(title: "Photo Source", message: " Please Choose Your Image", preferredStyle: .actionSheet)
    actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler:{  (action:UIAlertAction) in
        self.myPickerImage.sourceType = .camera
        self.myPickerImage.delegate = self
        self.myPickerImage.allowsEditing = true
        
        self.present(self.myPickerImage, animated: true, completion: nil)
       
             // self.present(imagePickerVC, animated: true, completion: nil)
          }))
    actionSheet.addAction(UIAlertAction(title: "Photo library", style: .default, handler:{  (action:UIAlertAction) in
        self.myPickerImage.sourceType = .photoLibrary
        self.myPickerImage.delegate = self
        self.myPickerImage.allowsEditing = true
      
        self.present(self.myPickerImage, animated: true, completion: nil)
    
    }))
    actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
    self.present(actionSheet, animated: true, completion: nil)
   
}

这也是我的图像选择器功能:

 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    
    if let image = info[.editedImage] as? UIImage {
        
        
        if let imageURL = info[.imageURL] as? URL {
           
            let asset = imageURL.description
        
            print("this is the asset \(asset)")
        }
        print("this isssssssssss \(info.values.description)")
        if (info[.mediaURL] != nil) {
                       print(" this is provided for you \(String(describing: info[.mediaURL]))")
                   }
        profileImage.image = image
        
    } 
}

这也是服务器上补丁的输入图像内部:

 func imageInput(asset : String) {
    let semaphore = DispatchSemaphore (value: 0)
    let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! + "/" + asset
    let parameters = [
      [
        "key": "image",
        "src": "\(path)",
        "type": "file"
      ]] as [[String : Any]]

    let boundary = "Boundary-\(UUID().uuidString)"
    var body = ""
    var error: Error? = nil
    for param in parameters {
      if param["disabled"] == nil {
        let paramName = param["key"]!
        print("params image key is \(paramName)")
        body += "--\(boundary)\r\n"
        body += "Content-Disposition:form-data; name=\"\(paramName)\""
        if param["contentType"] != nil {
          body += "\r\nContent-Type: \(param["contentType"] as! String)"
        }
        let paramType = param["type"] as! String
        if paramType == "text" {
          let paramValue = param["value"] as! String
            print("value of param is \(paramValue)")
          body += "\r\n\r\n\(paramValue)\r\n"
        } else {
        
                if path != nil {
                
                body += "; filename=\"\(path)\"\r\n"
                               + "Content-Type: \"content-type header\"\r\n\r\n \(path)\r\n"
                print("this is going to true")
                }
            
        }
      }
    }
    body += "--\(boundary)--\r\n";
    let postData = body.data(using: .utf8)

    var request = URLRequest(url: URL(string: "https://api.offernews.co/api/user")!,timeoutInterval: Double.infinity)
    request.addValue("baerar \(profileKeychain["token"]!)", forHTTPHeaderField: "Authorization")
    request.addValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")

    request.httpMethod = "PATCH"
    request.httpBody = postData

    let task = URLSession.shared.dataTask(with: request) { data, response, error in
      guard let data = data
        
        else {
        print(String(describing: error))
        semaphore.signal()
        return
      }
      print(String(data: data, encoding: .utf8)!)
      semaphore.signal()
    }

    task.resume()
    semaphore.wait()
}

非常感谢您的关注。

标签: iosswift

解决方案


推荐阅读