我有一個使用UICollectionView設計的聊天視圖。我已經成功實現了圖片上傳功能,只要用戶使用圖片選擇器點擊圖片,圖片就會上傳到服務器。現在我正在試圖爲每張圖片添加進度條來顯示上傳狀態。目前我所做的是,無論何時用戶使用UIImagePicker選擇一個圖像,我的單元格將獲得最新圖像的更新,而集合視圖會滾動到底部,並在URLsession委託中捕獲最後一個單元格並顯示進度,但問題是,當我在上傳過程中選擇另一個圖像時,兩個進度條顯示在最後一個單元格上。下面是我對圖像拾取和會議代表代碼在swift中顯示UICollectionView中的多個圖像上傳進度條
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
selectedImage = chosenImage
//Some other code to update cell data
picker.dismissViewControllerAnimated(true, completion: {() -> Void in
self.scrollToBottomAnimated(true)
})
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
print("To run image upload in background")
self.uploadImage(UIImageJPEGRepresentation(self.selectedImage!, 0.5)!, parameters: self.createDictionaryForUploadImageDetails()) { (responseDict, error) ->() in
if (responseDict != nil) {
print(responseDict!)
}
else
{
if error != nil
{
Utilities.showAlertViewMessageAndTitle((error?.localizedDescription)!, title: "Error", delegate: [], cancelButtonTitle: "OK")
}
}
}
})
}
NSURLSession代表
func URLSession(session: NSURLSession, task: NSURLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64)
{
let uploadProgress:Float = Float(totalBytesSent)/Float(totalBytesExpectedToSend)
let section = self.numberOfSectionsInCollectionView(self.chatCollectionView) - 1
let item = self.collectionView(self.chatCollectionView, numberOfItemsInSection:section)-1
let finalIndexPath = NSIndexPath(forItem: item, inSection: section)
if let selectedCell = chatCollectionView.cellForItemAtIndexPath(finalIndexPath)
{
selectedCell.progressView.hidden = false
selectedCell.progressView = Int(uploadProgress * 360)
let progressPercent = Int(uploadProgress*100)
selectedCell.progressView = Int(uploadProgress) == 1
print("Progress-\(progressPercent)% - \(progressView.hidden)")
}
}
我怎樣才能得到每個上傳正確的電池?
沒錯。換句話說,當細胞即將被顯示時,它將顯示模型(圖像)的當前狀態,例如,如果正在下載,您將顯示當前百分比,單元格將開始監聽模型更改(委託模式)。在每個時刻,由於單個單元格映射到單個模型對象,因此只能有一個模型委託集。 – Mercurial