ios - 将图像文件名修补到服务器 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)
}
如果您有任何想法,请分享它,感谢您花时间寻求帮助。
解决方案
推荐阅读
- c# - DataTable 列与 Winform 图表图例对齐
- java - 使用命令行将 Batch 变量作为参数传递给 Java 类
- wordpress - Wordpress:事件日历,ACF:基于 ACF 关系字段的 TEC 过滤条过滤器
- reactjs - 反应头盔的目的是什么?
- angular - 从父 MVC 应用程序打开 Angular 5 应用程序
- python - 我的井字游戏出现我不明白的 TypeError
- java - C# Windows 窗体用鼠标绘制图片框
- r - purr::pmap 不保留参数名称
- java - 检查枚举列表是否包含Java中的字符串
- keras - 具有多个滤波器的卷积层的输出形状