i have array of parameters and array of images each set of parameter contain one and only one image.my code
let imgData = UIImageJPEGRepresentation(imageView.image!, 0.2)!
Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(imgData, withName: "fileset",fileName: "file.jpg", mimeType: "image/jpg")
for (key, value) in params {
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
},
to:URLUpdateProfile,
method:.post,
headers:headers)
{ (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})
upload.responseJSON { response in
print(response.result.value)
}
case .failure(let encodingError):
print(encodingError)
}
}
with this code i am able to upload one image along with one parameter.but i want to send parameter in array and image in array too.is the way to upload array of params with array of image? if yes how to track image and parameter?
You can upload each image and its param in an Operation
. Your Operation
should look something like this:
class UploadImageOperation: Operation {
private var image: UIImage
init(withImage: UIImage) {
super.init()
image = withImage
}
override func main() {
let imgData = UIImageJPEGRepresentation(image, 0.2)!
Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(imgData, withName: "fileset",fileName: "file.jpg", mimeType: "image/jpg")
for (key, value) in params {
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
},
to:URLUpdateProfile,
method:.post,
headers:headers)
{ (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})
upload.responseJSON { response in
print(response.result.value)
}
case .failure(let encodingError):
print(encodingError)
}
}
}
}
Then you create the operations and add them to the queue like this:
let opQueue = OperationQueue()
opQueue.name = "imageUploadQueue"
opQueue.maxConcurrentOperationCount = 5 //number of images you want to be uploaded simultaneously
opQueue.qualityOfService = .background
for image in arrayOfImages {
let uploadImageOperation = UploadImageOperation(withImage: image)
opQueue.addOperation(uploadImageOperation)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.