我想用翠鸟下载多张图片,并通过页面控制(如 instagram 主页)在收藏视图中显示这些图片。为此,我创建了自定义图像视图。我尝试如下,但显示的图像都是相同的,即使 url 不同。我怎样才能解决这个问题?先感谢您!
import UIKit
import Kingfisher
class CustomImageView: UIImageView {
var lastUrlToLoad: String?
func loadMultipleImages(urlStrings: [String]) {
for urlString in urlStrings {
lastUrlToLoad = urlString
guard let url = URL(string: urlString) else { return }
let resouce = ImageResource(downloadURL: url, cacheKey: urlString)
KingfisherManager.shared.retrieveImage(with: resouce, options: nil, progressBlock: nil) { [weak self] (img, err, type, url) in
if err != nil {
return
}
if url?.absoluteString != self?.lastUrlToLoad {
return
}
DispatchQueue.main.async {
self?.image = img
}
}
}
}
}
编辑
我像这样使用这种方法。
class CollectionView: UICollectionViewCell {
@IBOutlet var imageView: CustomImageView!
var post: Post? {
didSet {
guard let urlStrings = post?.imageUrls else { return }
imageView.loadMultipleImages(urlStrings: urlStrings)
}
}
}