我collectionview
用来显示下载的URL标识的图像。这是我的代码:
var urlArray = ["url1", "url2", ..., "urln"]
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCell, for: indexPath) as? MyCell
cell.objectImageView.downloadedFrom(link: urlArray[indexPath.row], contentMode: UIViewContentMode.scaleAspectFit)
}
这是我的扩展代码,用于异步下载映像:
func downloadedFrom(url: URL, contentMode mode: UIViewContentMode = .scaleAspectFit) {
contentMode = mode
URLSession.shared.dataTask(with: url) { data, response, error in
guard
let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
let data = data, error == nil,
let image = UIImage(data: data)
else { return }
DispatchQueue.main.async() {
self.image = image
}
}.resume()
}
func downloadedFrom(link: String, contentMode mode: UIViewContentMode = .scaleAspectFit) {
guard let url = URL(string: link) else { return }
downloadedFrom(url: url, contentMode: mode)
}
当我滚动collectionview
图像时,每个单元格中的图像都在不断变化。我试着通过设置来解决它imageview
,以nil
它加载在之前cellForItemAt
的方法:
cell.objectImageView.image = nil
cell.objectImageView.downloadedFrom(link: urlArray[indexPath.row], contentMode: UIViewContentMode.scaleAspectFit)
但这是行不通的。如何解决这个问题?
设置nil
不会完全有帮助。主要是单元重用问题。
图像下载是异步的,这意味着它将在以后的时间完成。
如果您滚动并cell
在图像下载完成之前重新使用了a ,它将设置上一个下载调用的图像,从而引起您的连续更改,这肯定是故障的。
您将需要其他信息来跟踪此信息,以便从根本上确定完成的下载任务是用于imageView
还是已被重用,在这种情况下,将由另一个下载任务进行设置。
我们可以通过多种方式来执行此操作,但是在下面,我们将检查下载开始时的URL是否与下载完成时的URL相同。
简单的事情如下:
func downloadedFrom(url: URL, contentMode mode: UIViewContentMode = .scaleAspectFit) {
/*
1.
strUniqueIdentifier_Initial will be the url that caused the download to start.
A copy of this will be accessible in the closure later.
Also, we bind this to the imageView for case handling in the closure.
*/
let strUniqueIdentifier_Initial = url.absoluteString
self.accessibilityLabel = strUniqueIdentifier_Initial
contentMode = mode
let dataTask = URLSession.shared.dataTask(with: url) { data, response, error in
guard
let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
let data = data, error == nil,
let image = UIImage(data: data)
else { return }
/*
2.
strUniqueIdentifier_Initial is a copy of the url from the start of the function
strUniqueIdentifier_Current is the url of the current imageView as we use self
so if the imageView is reused, this method will be called on it again and at
that time it it's binded url string will be for the latest download call
If there's mismatch then the imageView was reused
*/
let strUniqueIdentifier_Current = self.accessibilityLabel
if strUniqueIdentifier_Initial != strUniqueIdentifier_Current {
//previous download task so ignore
return
}
DispatchQueue.main.async() {
self.image = image
}
}
dataTask.resume()
}
您可以优化逻辑以取消先前的下载,但这是解决核心问题的基本方法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句