gpt4 book ai didi

swift - 多部分图像 Alamofire 4 swift 3

转载 作者:行者123 更新时间:2023-11-28 08:24:14 25 4
gpt4 key购买 nike

当我尝试将图像更新到我的 Db 时遇到了一个大问题。

我必须上传一张图片和一些字符串值。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
imageCropped = selectedImage
self.sendImageWithMultipart()
imagePicker.dismiss(animated: true, completion: { _ in })
}
}

func sendImageWithMultipart() {
Alamofire.upload(multipartFormData: {
multipartFormData in
multipartFormData.append("\(HGYGjihf746fg743g)".data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName :"auth_token")
multipartFormData.append("\(0)".data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName :"os")
multipartFormData.append("\(10.0.3)".data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName :"os_version")
multipartFormData.append("\(3.0.3)".data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName :"app_version")

if let photo = self.imageCropped, let jpegImage = UIImageJPEGRepresentation(photo, 50.0) {
print(jpegImage)
multipartFormData.append(jpegImage, withName: "profile_photo", mimeType: "image/*")
}
}, to: "\(Constants.GENERAL_ADDRESS_2)\(Constants.API_MODIFYUSER_PROFILE_PHOTO)", method:.post, headers:["Content-Type":"multipart/form-data; charset=utf-8; boundary=__X_PAW_BOUNDARY__"], encodingCompletion: {
encodingResult in
switch encodingResult {
case .success(request: let upload, streamingFromDisk: _, streamFileURL: _):
upload.validate().responseJSON {
response in
if response.result.isFailure {
debugPrint(response)
} else {
debugPrint(response)
}
}
case .failure(let encodingError):
NSLog((encodingError as NSError).localizedDescription)
}
})
}

但结果是这样的:

[Request]: "\(Constants.GENERAL_ADDRESS_2)\(Constants.API_MODIFYUSER_PROFILE_PHOTO)"
[Response]: <NSHTTPURLResponse: 0x17023c0e0> { URL: "\(Constants.GENERAL_ADDRESS_2)\(Constants.API_MODIFYUSER_PROFILE_PHOTO)" } { status code: 500, headers {
"Cache-Control" = "no-cache, private";
Connection = close;
"Content-Length" = 5545;
"Content-Type" = "text/html; charset=UTF-8";
Date = "Thu, 10 Nov 2016 10:49:00 GMT";
Server = "Apache/2.4.18 (Amazon) OpenSSL/1.0.1k-fips PHP/5.6.21";
"X-Powered-By" = "PHP/5.6.21";
} }
[Data]: 5545 bytes
[Result]: FAILURE: responseValidationFailed(Alamofire.AFError.ResponseValidationFailureReason.unacceptableStatusCode(500))
[Timeline]: Timeline: { "Request Start Time": 500467740.667, "Initial Response Time": 500467740.793, "Request Completed Time": 500467743.733, "Serialization Completed Time": 500467743.733, "Latency": 0.126 secs, "Request Duration": 3.066 secs, "Serialization Duration": 0.000 secs, "Total Duration": 3.066 secs

问题是我如何转换图像,或内容类型或什么?

谢谢

最佳答案

这可能会解决您的问题:

let endpoint = "https://api.blah/images/" + id + "/documents"

let imageData = UIImagePNGRepresentation(image)

Alamofire.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(type.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName :"type")
multipartFormData.append(imageData!, withName: "file.png", mimeType: "image/png")
}, to: endpoint, method:.post, headers:headers, encodingCompletion: { encodingResult in
switch encodingResult {
case .success(request: let upload, streamingFromDisk: _, streamFileURL: _):
upload.validate().responseJSON { (response) in
if response.result.isFailure {
debugPrint(response)
} else {
debugPrint(response)
}
}
case .failure(let encodingError):
NSLog((encodingError as NSError).localizedDescription)
}
})

似乎到达了我的服务器,但就我而言,我得到了 400 返回(可能 tv4 验证失败)。

关于swift - 多部分图像 Alamofire 4 swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40525951/

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