如何解决将图像文件名修补到服务器 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()
}
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 举报,一经查实,本站将立刻删除。