我目前正在尝试使用Github上的Finjinon sdk发送多张照片,以便能够拍摄多张照片并将其发送到我的服务器。我正在使用alamofire SDK处理实际图片的发送。但是,每次发送图片时,它会随机复制其中一张图片,然后发送其余的图片。老实说,我完全陷入了困境,我什至不知道从哪里开始解决这个问题。
for i in 0..<imageResultsFilter.count {
print(i)
let imageForUpload = imageResultsFilter[i]
if let imageData = UIImageJPEGRepresentation(imageForUpload, 1) {
multipartFormData.append(imageData, withName: "file\(i)", fileName:"image\(i).jpeg", mimeType: "image/jpeg")
}
}
这是我的for循环,我已经检查了它,我相当肯定这不是问题。
这就是我将图片发送到服务器的方式
// Uploading files to server using Alamofire.
Alamofire.upload(multipartFormData: {
multipartFormData in
for (key, parValue) in parameters {
multipartFormData.append((parValue.data(using: .utf8))!, withName: key)
}
var imageResultsFilter = [UIImage]()
imageResultsFilter = self.imageArray.enumerated().filter({ index, _ in
index % 2 != 0
}).map({ $0.1 })
print(self.imageArray.count)
for i in 0..<imageResultsFilter.count {
print(i)
let imageForUpload = imageResultsFilter[i]
if let imageData = UIImageJPEGRepresentation(imageForUpload, 1) {
multipartFormData.append(imageData, withName: "file\(i)", fileName:"image\(i).jpeg", mimeType: "image/jpeg")
}
}
}, to: urlPath, encodingCompletion: {
encodingResult in
switch encodingResult {
case .success(let upload, _, _) :
upload.responseJSON(completionHandler: {
response in
// Json response from the server
if let jsonValue = response.result.value {
let json = JSON(jsonValue)
// Parsing the response from the server
for (keyTitle, info) in json {
self.alertTitle = "\(keyTitle)"
self.messageText = "\(info)"
}
}
if response.response?.statusCode == 500 {
// Alerts user that there was an Error in uploading the file.
let alert = UIAlertController(title: self.alertTitle, message: self.messageText, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Okay", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
} else if response.response?.statusCode == 200 {
// Alerts the user that the file succesfully uploaded to server.
let alert = UIAlertController(title: self.alertTitle, message: self.messageText, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Okay", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
})
case .failure(let encodingError):
print("error:\(encodingError)")
}
})
}
老实说,我不知道哪里出了问题。 它不会给我任何错误,我使用for循环,因为每次拍照时,它实际上同时存储两个。 例如,假设我拍了3张照片。数组将以0-5填充,我将发送其他所有图片。但是,当我回去再拍一张照片时,它将从0-5变为0-7。