0

Requirement

Application which upload multiple images on server with parameters.Need to show progress of each images in UITableViewCell

Implemented till now

Able to upload multiple images on server. Shown image in UITableViewCell.Put static label for showing image upload progress (i.e. Uploading...20%) beside UIImageView

Remaining stuff

Show progress of all images at a time. After upload image 100%, it will be remove from UITableViewCell

What i did in code

I am using native URLSession for uploading multiple image on server.

@IBAction func actionDownloadAll() {
     for image in self.arrImages {
         makeRequestWithImageUpload(image: image,......)
     }
}
func makeRequestWithImageUpload(image: UIImage,
                                                imageKey: String,
                                                endPoint:String,
                                                withHttpMethod:HTTPMethod,
                                                withParameters:[String:Any]? = nil,
                                                authorization:String? = nil,
                                                onComplete:@escaping (Result<Data?,RequestError>) -> Void) {
        
        guard let url = URL(string: Urls.baseUrl + currentAPIVersion + endPoint) else { return }
        
        self.authorization = authorization
        
        let boundary = generateBoundary()
        
        var request = URLRequest(url: url)
        
        guard let mediaImage = Media(withImage: image, forKey: imageKey) else { return }
        
        request.httpMethod = withHttpMethod.rawValue
        
        request.allHTTPHeaderFields = [
            "Content-Type": "multipart/form-data; boundary=\(boundary)",
            "Authorization": self.authorization!
        ]
        
        let dataBody = createDataBody(withParameters: withParameters, media: [mediaImage], boundary: boundary)
        request.httpBody = dataBody
            
        let session = URLSession(configuration: .default, delegate: self, delegateQueue: .main)
        
        let task = session.dataTask(with: request) { (data, response, error) in
            
            guard let response = response as? HTTPURLResponse else {
                onComplete(.failure(.init(message: "Did not get a http response")))
                return
            }
            
            if let error = error as NSError? {
                onComplete(.failure(.init(message: "\(response.statusCode): \(error.localizedDescription)")))
                return
            }
            
            onComplete(.success(data))
        }
        task.resume()
    }

func urlSession(_ session: URLSession,task: URLSessionTask,didSendBodyData bytesSent: Int64,totalBytesSent: Int64,totalBytesExpectedToSend: Int64) {
        let progress = Double(totalBytesSent) / Double(totalBytesExpectedToSend)
        print("in progress...\(progress)")
    }

Logic will be like

I have an idea that i have to pass some unique id at the time of request and when i get response then i have to bind that unique id with progress value. But do not know how to execute this idea. I heard about task.taskIdentifier but how to use this to achieve my goal that i don't know.I know i can achieve with above code but stuck in showing progress of each images.

Thanks in advance.

Birju
  • 1,132
  • 11
  • 32

0 Answers0