我正在尝试使用 Alamofire 上传一组图像但出现错误
I'm trying to upload an array of images using Alamofire but getting error
我正在尝试使用 Alamofire 4.8.2 将一组图像上传到服务器
函数如下:
func uploadMultiplePhotos(centreId: Int, imagesArray: [UIImage]) {
let parameters = ["ec_id": centreId, "uploaded_image": imagesArray] as [String : Any]
Alamofire.upload(multipartFormData: { (multipartFormData : MultipartFormData) in
let count = imagesArray.count
for i in 0..<count{
multipartFormData.append(imagesArray[i], withName: "photo[\(i)]", fileName: "photo\(i).jpeg", mimeType: "image/jpeg")
}
for (key, value) in parameters {
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
}
print(multipartFormData)
}, to: storeCentreImageURL) { (result) in
switch result {
case .success(let upload, _ , _):
upload.uploadProgress(closure: { (progress) in
print("uploding: \(progress.fractionCompleted)")
})
upload.responseJSON { response in
print(response.result.value!)
}
case .failure(let encodingError):
print("failed")
print(encodingError)
}
}
}
在以下行中出现错误:
multipartFormData.append(imagesArray[i], withName: "photo[\(i)]", fileName: "photo\(i).jpeg", mimeType: "image/jpeg")
Cannot invoke 'append' with an argument list of type '(UIImage, withName: String, fileName: String, mimeType: String)'
您需要将图像转换为数据
imagesArray.indices.forEach {
multipartFormData.append(imagesArray[[=10=]].jpegData(compressionQuality:0.8)!, withName: "photo[\([=10=])]", fileName: "photo\([=10=]).jpeg", mimeType: "image/jpeg")
}
我正在尝试使用 Alamofire 4.8.2 将一组图像上传到服务器
函数如下:
func uploadMultiplePhotos(centreId: Int, imagesArray: [UIImage]) {
let parameters = ["ec_id": centreId, "uploaded_image": imagesArray] as [String : Any]
Alamofire.upload(multipartFormData: { (multipartFormData : MultipartFormData) in
let count = imagesArray.count
for i in 0..<count{
multipartFormData.append(imagesArray[i], withName: "photo[\(i)]", fileName: "photo\(i).jpeg", mimeType: "image/jpeg")
}
for (key, value) in parameters {
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
}
print(multipartFormData)
}, to: storeCentreImageURL) { (result) in
switch result {
case .success(let upload, _ , _):
upload.uploadProgress(closure: { (progress) in
print("uploding: \(progress.fractionCompleted)")
})
upload.responseJSON { response in
print(response.result.value!)
}
case .failure(let encodingError):
print("failed")
print(encodingError)
}
}
}
在以下行中出现错误:
multipartFormData.append(imagesArray[i], withName: "photo[\(i)]", fileName: "photo\(i).jpeg", mimeType: "image/jpeg")
Cannot invoke 'append' with an argument list of type '(UIImage, withName: String, fileName: String, mimeType: String)'
您需要将图像转换为数据
imagesArray.indices.forEach {
multipartFormData.append(imagesArray[[=10=]].jpegData(compressionQuality:0.8)!, withName: "photo[\([=10=])]", fileName: "photo\([=10=]).jpeg", mimeType: "image/jpeg")
}