多个图像上传进度问题



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

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

编辑:

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

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

例如,你有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)

我找到了一个解决方案。

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

}

相关内容

  • 没有找到相关文章

最新更新