多张图片上传进度问题

Multiple Image Upload Progression problems

我正在尝试上传几张单独的图片,只要我在有效时上传它们 1 张即可。但是一旦我尝试上传下一张或多张图片,问题就开始了。 我在系列中上传的每张图片都会根据上传的最新图片的进度更新所有图片。 当我在最后一张上传完成之前拍了不止一张图片时,它会通过随机显示各种上传进度号来干扰其他图片的进度。

这就是我更新 UI 的方式:

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {

    let cell = tableView.dequeueReusableCell(withIdentifier: "uploadQueueCell", for: indexPath) as! UploadQueueCustomCell

        if cell.progress.text?.lowercased() == "percentage".lowercased()
        {
            cell.progress.text = "0"
        }
            if indexPath.row < uploader.GetUploadQueue().Count()
            {
                let item = uploader.GetUploadQueue().Get(pos: indexPath.row)
                cell.filename.text = item._FileName
                let percentage = String(item._Percentage)
                cell.progress.text = percentage
                cell.cell_image.image = UIImage(data: item._ImageData)
            }
            updateView()
               return cell

    }
    public func updateView() {
            DispatchQueue.main.async
            {
                self.tableView.reloadData()
            }
    }

我的数组中的各个项目是这样存储的:

public class UploadQueueCellData
{
    public let _FileName:String
    public let _UploadTaskDelegate:URLSessionTaskDelegate
    public let _ImageData:Data
    public let _TaskIdentifier:Int

    public init(fileName:String,imageData:Data,uploadTaskDelegate:URLSessionTaskDelegate,taskIdentifier: Int)
    {
        _FileName = fileName
        _ImageData = imageData
        _UploadTaskDelegate = uploadTaskDelegate
        _TaskIdentifier = taskIdentifier
    }
    public var _Percentage:Int
    {
        get
        {
            let test = _UploadTaskDelegate as! UploadDelegate
            let returnval = test._percentage
            return returnval
        }

    }   
}

和我的代表上传进度

    public class UploadDelegate: URLSessionUploadTask, URLSessionTaskDelegate {
    public var _response:HTTPURLResponse = HTTPURLResponse()
    public var _error:String? = nil
    public var _percentage:Int = 0
    public var _Filename:String? = nil
    public var _urlSessionTask:URLSessionTask? = nil


    public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)
    {
        _error = error.debugDescription
    }
    public func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64)//->Int
    {
        //Progression
        let progress = Float(totalBytesSent)/Float(totalBytesExpectedToSend)
        _urlSessionTask = task
        DispatchQueue.main.async
        {
            self._percentage = Int(progress * 100)
        }
    }
}

我不知道如何分离各个上传并跟踪它们的进度我所做的一切似乎都在努力只能跟踪最后一次上传。或使用随机进度编号而不是属于单个上传的进度更新所有这些。有没有办法让我的模型真正分离个人上传并分别跟踪它们?我将如何去做呢?

编辑:

我想我应该补充一点,我一次发送一张图像,但由于连接或速度较慢,最终可能会出现尚未完成的已发送项目的缓存队列。我只是想显示已发送的单个项目的进度。

你的解决方案在于递归。只是通过示例给出基本思想,希望你深入研究它并根据需要创建。

例如。你有 imageArray10 张图片

        func uploadImageATIndex(index:Int,uploadArray:[UIImage]){
                var position = index

                    self.uploadImageRemote(uploadArray[position]){ dataImage, error -> Void in
                                  //callback when image is uploaded 

                 //check whether position is the last index or not.if not than again call self
                        if !(position == uploadArray.count - 1){
                            position = position+1
                                  //increase the position and again call the self
                            self.uploadImageATIndex(position,uploadArray: uploadArray)
                        }else{
                         //when index becomes equal to the last index return
                            return
                        }
                    }
            }

通过给定 index 0

仅调用此方法一次
self.uploadImageATIndex(0,imageArray)

我找到了解决办法。

我的解决方案是尝试跟踪我收到的是哪个文件的进度。我知道这可能不是一个很好的解决方案,但它在 atm 上工作我是这样做的:

 public func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64)//->Int
        {
            //Progression
            _Filename =  task.currentRequest?.value(forHTTPHeaderField: "filename")
            let progress = Float(totalBytesSent)/Float(totalBytesExpectedToSend)
            _urlSessionTask = task
            DispatchQueue.main.async
            {
                self.objQueue?.Get(filename: self._Filename!)._Percentage = Int(progress * 100)
              //  self._percentage = Int(progress * 100)
            }
        }

其余的和之前差不多

有一个小例外:

public class UploadQueueCellData//:NSObject, UploadURLSessionTaskDelegate
{
    public let _FileName:String
    public let _UploadTaskDelegate:URLSessionTaskDelegate
    public let _ImageData:Data
    public let _TaskIdentifier:Int
    public init(fileName:String,imageData:Data,uploadTaskDelegate:URLSessionTaskDelegate,taskIdentifier: Int)
    {
        _FileName = fileName
        _ImageData = imageData
        _UploadTaskDelegate = uploadTaskDelegate
         _TaskIdentifier = taskIdentifier

    }
    private var intPercentage:Int = -1
    public var _Percentage:Int
    {
        get
        {
             return self.intPercentage
        }
        set
        {
            self.intPercentage = newValue

        }
    }



}