gpt4 book ai didi

ios - Swift 3 上传图片问题(格式)

转载 作者:行者123 更新时间:2023-11-28 16:01:59 24 4
gpt4 key购买 nike

我必须将图片上传到后端 (PHP),并且必须使用参数名称发送它:“图片”

func uploadImage(token: String, userID: Int, imgStr: String,
successBlock: @escaping (JSON, Int) -> (),
failureBlock: @escaping (String) -> ())
{
let params: [String: Any] = [
"picture" : imgStr
]

Alamofire.request( "\(API_URL)" + "users/\(userID)",
method: .put,
parameters: params,
encoding: JSONEncoding.default,
headers: Headers().withToken(token: token)).responseJSON
{ response in

print(response)
print(response.result)
print(response.request)
print(response.response)

response.result.error != nil
? (failureBlock(response.result.error!.localizedDescription))
: (successBlock(JSON(response.result.value),
(response.response?.statusCode)!))
}
}

但是我收到了这条消息

JSON: {
"picture" : [
"The picture must be a file of type: jpeg, jpg, png."
]
}

有没有办法将 UIImage 转换为 png/jpeg 格式的字符串?

非常感谢!

最佳答案

这是你如何将图像以文件格式发送到服务器

let headers: HTTPHeaders = [String:String]
let URL = try! URLRequest(url: yourURL, method: .put, headers: headers)

Alamofire.upload(multipartFormData: { multipartFormData in
if let _image = UIImage(named:"") {
if let imageData = UIImagePNGRepresentation(_image) {
multipartFormData.append(imageData, withName: "signImg", fileName: "picture.png", mimeType: "image/png")
}
}
}, with: URL, encodingCompletion: {
encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
debugPrint("SUCCESS RESPONSE: \(response)")
}
case .failure(let encodingError):
// hide progressbas here
print("ERROR RESPONSE: \(encodingError)")
}
})

关于ios - Swift 3 上传图片问题(格式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40891738/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com