【发布时间】:2021-09-27 16:32:36
【问题描述】:
实际上我想更改我的个人资料的照片,但在我更改 json 对象之一中的“profileImage”的值之前,上传了一张照片,我想用用户选择的照片更改 json 对象的值来自照片库,“profilePhoto”里面的照片(照片地址)必须改变,你有什么意见?
这是用户 json 格式:
["country": {
"__v" = 0;
"_id" = 606836832ed79e4194e98849;
name = emirate;
}, "_id": 60f27a3d6aba1b154a394acc, "email": leopard@gmail.com, "name": Leo program, "city": {
"__v" = 0;
"_id" = 6068368d2ed79e4194e9884a;
countryId = 606836832ed79e4194e98849;
title = Dubai;
},
"address": this is the test address from programmer, "userName": Leopard,
"profileImage": images/profile/2021-07-19T10-18-48.901Z-20px-Flag_of_the_Valencian_Community_(2x3).svg.png,
"telephone": 09336322332]
这也是我的代码,http 请求是 PATCH,配置文件图像的关键是“图像”:
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
if asset.contains("file://") {
let changableURl = asset.replacingOccurrences(of: "file://", with: "")
URLOfImage = changableURl
//let changableURl = asset
print("this is image url like this: \(changableURl)")
let parameters = [
[
"key": "image",
"src": "\(changableURl)",
"type": "file"
]] as [[String : Any]]
//changeImage(parameters: parameters)
uploadImage(paramName: "image", fileName: changableURl, image: image)
}
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]))")
}
profileImage.image = image
// if saveImage(image: image) == true {
// print("this successeful photo image")
//
// }
} else if let image = info[.originalImage] as? UIImage {
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: "")
//let changableURl = asset
print(changableURl)
let parameters = [
[
"key": "image",
"src": "\(changableURl)",
"type": "file"
]] as [[String : Any]]
//changeImage(parameters: parameters)
uploadImage(paramName: "image", fileName: changableURl, image: image)
}
print("this is the asset \(asset)")
// print(asset?.value(forKey: "filename"))
}
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)
}
这也是我在 json 上上传图片的内部:
func uploadImage(paramName: String, fileName: String, image: UIImage) {
let url = URL(string: "https://api.offernews.co/api/user")
// generate boundary string using a unique per-app string
let boundary = UUID().uuidString
let session = URLSession.shared
// Set the URLRequest to POST and to the specified URL
var urlRequest = URLRequest(url: url!)
urlRequest.httpMethod = "PATCH"
// Set Content-Type Header to multipart/form-data, this is equivalent to submitting form data with file upload in a web browser
// And the boundary is also set here
urlRequest.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
var data = Data()
// Add the image data to the raw http request data
data.append("\r\n--\(boundary)\r\n".data(using: .utf8)!)
data.append("Content-Disposition: form-data; name=\"\(paramName)\"; filename=\"\(fileName)\"\r\n".data(using: .utf8)!)
data.append("Content-Type: image/png\r\n\r\n".data(using: .utf8)!)
data.append(image.pngData()!)
data.append("\r\n--\(boundary)--\r\n".data(using: .utf8)!)
// Send a POST request to the URL, with the data we created earlier
session.uploadTask(with: urlRequest, from: data, completionHandler: { responseData, response, error in
if error == nil {
let jsonData = try? JSONSerialization.jsonObject(with: responseData!, options: .allowFragments)
if let json = jsonData as? [String: Any] {
print(" this leatherface vs json : \(json)")
//guard case let json["profileImage"] = UIImage(contentsOfFile: self.URLOfImage)! as UIImage else {
// return
// }
var profileImageName = json["profileImage"] as! UIImage
profileImageName = UIImage(contentsOfFile: self.URLOfImage)!
// json["profileImage"] = profileImageName
//json["profileImage"] = UIImage(contentsOfFile: URLOfImage)
}
}
}).resume()
}
非常感谢您的帮助
【问题讨论】:
-
"this my json format" 不是 JSON 格式。
-
@ElTomato 你的右哥,但是我尝试改变“photoProfile”的值,你有什么意见?
-
但是为什么您在上传图像时使用本地路径。上传图片将返回
url。 -
@jatinfl 亲爱的朋友其实我是初学者,我不知道该怎么办我需要一些帮助
-
请先在邮递员上运行该(上传图片)api并检查响应。