翠鸟用自定义图像视图下载多个图像(Kingfisher download multiple images with custom image view)

编程入门 行业动态 更新时间:2024-10-24 22:26:36
翠鸟用自定义图像视图下载多个图像(Kingfisher download multiple images with custom image view)

我想用翠鸟下载多个图像,并使用页面控制显示收藏视图上的图像(如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() }

更多推荐

本文发布于:2023-08-06 22:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1456789.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   翠鸟   多个   自定义   视图

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!