0

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?

Prashant Ghimire
  • 518
  • 4
  • 20

1 Answers1

1

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)
}
Max Pevsner
  • 4,098
  • 2
  • 18
  • 32
  • how about param each image has its own param.and want to send them in array. – Prashant Ghimire May 16 '17 at 04:25
  • @PrashantGhimire you can do it the same way. – Max Pevsner May 16 '17 at 05:43
  • can you please provide me example for params too. actually i am stuck at this point.my problem is that i have have params and thats in array too ie let params :Parameters = ["id" :[]), "name":[""], "dob":[""]]].and i am not able to do this for (key, value) in params { multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key) } it get crash because our value is not string its array of data. – Prashant Ghimire May 16 '17 at 08:43
  • @PrashantGhimire I believe you should create a separate question for that. It's another issue. I'll be glad to help you there. – Max Pevsner May 16 '17 at 09:11
  • please check my question i have ask that too.array of params and image – Prashant Ghimire May 16 '17 at 09:25