我想用翠鸟下载多个图像,并使用页面控制显示收藏视图上的图像(如instagram home feed)。 为此,我创建了自定义图像视图。 我尝试了下面的内容,但显示的图片都是一样的,即使网址不同。 我怎样才能解决这个问题? 先谢谢你!
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) } } }I would like to download multiple images with kingfisher and display those on collection view with page control (like instagram home feed). To do that I created custom image view. I tried like below but images being shown are all the same even though urls are different. How can I fix this? Thank you in advance!
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 } } } } }Edit
I use this method like so.
class CollectionView: UICollectionViewCell { @IBOutlet var imageView: CustomImageView! var post: Post? { didSet { guard let urlStrings = post?.imageUrls else { return } imageView.loadMultipleImages(urlStrings: urlStrings) } } }最满意答案
问题在于您试图在单个图像视图中显示多个图像。 结果,所有图像都被下载,但仅显示最后一次检索的图像。 您可能想要使用照片获取一些收藏视图,其中:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return imageUrls.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { //dequeueReusableCell with imageView cell.imageView.kf.setImage(with: imageUrls[indexPath.row]) return cell }或者,您可以遵循UICollectionViewDataSourcePrefetching来添加图像预取,Kingfisher也支持该预取:
collectionView.prefetchDataSource = self func collectionView(_ collectionView: UICollectionView, prefetchItemsAt indexPaths: [IndexPath]) { ImagePrefetcher(urls: indexPaths.map { imageUrls[$0.row] }).start() }The problem is that you are trying to display multiple images in a single image view. As a result, all images are downloaded but only last retrieved image is being displayed. You probably want to have some collection view with photos where:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return imageUrls.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { //dequeueReusableCell with imageView cell.imageView.kf.setImage(with: imageUrls[indexPath.row]) return cell }Optionally you could conform to UICollectionViewDataSourcePrefetching to add image prefetching, which is also supported by Kingfisher:
collectionView.prefetchDataSource = self func collectionView(_ collectionView: UICollectionView, prefetchItemsAt indexPaths: [IndexPath]) { ImagePrefetcher(urls: indexPaths.map { imageUrls[$0.row] }).start() }更多推荐
发布评论