多个图像上传进程问题

时间:2017-03-23 11:59:17

标签: ios swift3 nsurlsession urlsession urlsessiontask

我正在尝试上传几个单独的图片,只要我在它工作时上传它们。但是,一旦我尝试上传下一张图片或多张图片,问题便会开始。 我在系列中上传的每张图片都会根据上传的最新图片的进展更新所有图片。 当我在上传最后一张图片之前拍摄多张图片时,会通过随机显示各种上传进度数来扰乱其他图片的进展。

这就是我更新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)
        }
    }
}

我不知道如何分离单个上传并跟踪他们的进展我所做的一切似乎都只是为了能够跟踪上次上传。或使用随机进展编号更新所有这些编号,而不是属于单个上传的进展。有没有办法让我的模型分开单独的上传并分别跟踪它们?我该怎么做呢?

修改

我想我应该补充一点,我一次发送一张图片,但由于连接速度或速度慢,最终可能会收到仍未完成的已发送项目的缓存队列。我只是想显示已经发送的各个项目的进展。

2 个答案:

答案 0 :(得分:0)

你的解决方案在于递归。只需要给出基本的想法,例如希望你深入了解并根据需要创建。

例如。你有 imageArray 10张图片

        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
                        }
                    }
            }

仅通过提供索引0

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

答案 1 :(得分:0)

我找到了解决方案。

我的解决方案是尝试跟踪我正在接收进展的文件。我知道这可能不是一个简洁的解决方案,但它正在工作,我是这样做的:

 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

        }
    }



}