Swift 3 Alamofire 4使用参数上传图像数组 [英] Swift 3 Alamofire 4 Upload array of images with parameters
本文介绍了Swift 3 Alamofire 4使用参数上传图像数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Alamofire 4和Swift 3上传一系列图片以及一些参数.
I'm trying to upload an array of pictures along with some parameters using Alamofire 4 and Swift 3.
参数似乎起作用,因为更新已完成,但是图像未到达服务器
The parameters seem to work because the update is done, but the images don't get to the server
在邮递员中,我可以毫无问题地做到这一点:
In Postman I can do this fine with no problem:
到目前为止,这是我的代码:
This is my code so far:
let parameters = [
"service_request_id" : servicesID,
"status_id" : "4",
]
Alamofire.upload(
multipartFormData: { multipartFormData in
for (key,value) in parameters {
if let value = value as? String {
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
}
for (image) in self.imagesArray {
if let imageData = UIImageJPEGRepresentation(image, 1) {
multipartFormData.append(imageData, withName: "image", fileName: "image.jpeg", mimeType: "image/jpeg")
}
}
},
to: ConnectionWS.UpdateServicesURL,
method: .put,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print(progress)
})
upload.responseJSON { response in
// If the request to get activities is succesfull, store them
if response.result.isSuccess{
print(response.debugDescription)
alert.dismiss(animated: true, completion:
{
self.dismiss(animated: true, completion:
{
self.delegate?.statusChanged(IsFinish: false)
})
})
// Else throw an error
} else {
var errorMessage = "ERROR MESSAGE: "
if let data = response.data {
// Print message
let responseJSON = JSON(data: data)
if let message: String = responseJSON["error"]["message"].string {
if !message.isEmpty {
errorMessage += message
}
}
}
print(errorMessage) //Contains General error message or specific.
print(response.debugDescription)
}
alert.dismiss(animated: true, completion:
{
self.dismiss(animated: true, completion:nil)
})
}
case .failure(let encodingError):
print("FALLE ------------")
print(encodingError)
}
}
)
我做错什么了吗?
请帮助.
推荐答案
所以错误出在服务器端,图像的大小限制非常小,这就是为什么它们没有保存的原因.我从
So the error was from the server side, that had a very small size limit for images and that's why they weren't saving. I updated the compression for the JPEG images from
if let imageData = UIImageJPEGRepresentation(image, 1)
到
if let imageData = UIImageJPEGRepresentation(image, 0.6)
现在工作正常.
感谢@Sneak提供的链接.
Thank you @Sneak for your links.
这篇关于Swift 3 Alamofire 4使用参数上传图像数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文