微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将图像文件名修补到服务器 Swift 中很重要

如何解决将图像文件名修补到服务器 Swift 中很重要

基本上是这张个人资料图片,当我从库中添加图片时,我尝试修补图片文件名并将其保存在服务器上,但出现此错误:“在解开可选值时意外发现 nil:文件”行中下面我签名:我不知道为什么会这样:

这是我在服务器上尝试修补图像文件名的功能

func changeImage(parameters : [[String: Any]]) {
    var semaphore = dispatchSemaphore (value: 0)

   

    let boundary = "Boundary-\(UUID().uuidString)"
    var body = ""
    var error: Error? = nil
    for param in parameters {
      if param["disabled"] == nil {
        let paramName = param["key"]!
        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
          body += "\r\n\r\n\(paramValue)\r\n"
        } else {
          let paramSrc = param["src"] as! String

            let fileData = try? NSData(contentsOfFile: paramSrc,options: [])
          as Data
// @@@@@@ this the line that got me error on debug area 
            let fileContent = String(data: fileData! as Data,encoding: .utf8)
          body += "; filename=\"\(paramSrc)\"\r\n"
            + "Content-Type: \"content-type header\"\r\n\r\n\(fileContent)\r\n"
        }
      }
    }
    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(" this is error that you need to kNow  \(String(describing: error))")
        semaphore.signal()
        return
      }
      print(" this the data that you need to complete  \(String(data: data,encoding: .utf8)!)")
      semaphore.signal()
    }

    task.resume()
    semaphore.wait()
}

当我调用 top 函数时,这也是我的选择器图像:

 func imagePickerController(_ picker: UIImagePickerController,didFinishPickingMediawithInfo info: [UIImagePickerController.InfoKey : Any]) {
    
    if let image = info[.editedImage] as? UIImage {
        profileImage.image = image
        
        if let imageURL = info[.imageURL] as? URL {
           // let result = PHAsset.fetchAssets(withALAssetURLs: [imageURL],options: nil)
            let asset = imageURL.description
            if asset.contains("file://") {
                let changableuRl = asset.replacingOccurrences(of: "file://",with: "")
                print(changableuRl)
                let parameters = [
                         [
                           "key": "image","src": "\(changableuRl)","type": "file"
                         ]] as [[String : Any]]
               changeImage(parameters: parameters)
            }
            
            print("this is the asset \(asset)")
           // print(asset?.value(forKey: "filename"))

        }
     // print("  this is url of image :   \(info.description) ")
        print("this isssssssssss \(info.values.description)")
        if (info[.mediaURL] != nil) {
                       print(" this is provided for you \(String(describing: info[.mediaURL]))")
                   }
        
        if  saveImage(image: image) == true {
            
            print("this successeful photo image")
            
        }
    } else if let image = info[.originalImage] as? UIImage {
        print("   this is a test   \(info.values.description)")
        if (info[.mediaURL] != nil) {
            print(" this is provided for you \(String(describing: info[.mediaURL]))")
        }

        print("  this is url of image :   \(info.description) ")

        profileImage.image = image
        
    }
    dismiss(animated: true,completion: nil)
         
}    

如果您有任何想法,请分享出来,感谢您抽出宝贵时间提供帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?