0

我正在尝试上传几个单独的图像,只要我在它工作时上传它们 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)
        }
    }
}

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

编辑:

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

4

2 回答 2

0

您的解决方案在于递归。仅通过示例给出基本想法,希望您深入研究并根据需要进行创建。

例如。你有10张图片的imageArray

        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)
于 2017-03-23T13:01:44.547 回答
0

我找到了解决方案。

我的解决方案是尝试跟踪我收到进度的文件。我知道这可能不是一个简洁的解决方案,但它在工作 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

        }
    }



}
于 2017-03-27T08:08:02.137 回答