使用翠鸟从网上获取图像后将图像保存在数组中

saving images in an array after getting them form the web using kingfisher

我正在使用 kingfisher 库从 Web 获取图像并在图像视图中显示它们。每 5 秒我就会得到一个新图像并显示它。我想获取图像,将它们保存在一个数组中然后显示它们,以免每次都从网络上获取图像。只需一次获取它们并将它们存储在一个数组中,然后从数组中读取它们。 我现在的代码:

    var imagesURL = Array<String>() // contains the images urls
    var counter = 0
.
.

func fireTimer()  {
        timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(setImagesInImageView), userInfo: nil, repeats: true)
    }

    @objc func setImagesInImageView()  {
              let url = URL(string: self.imagesURL[counter] )
             self.imageDisplayer.kf.setImage(with: url)
            counter = counter + 1
        }

有什么帮助吗?

Kingfisher 将所有加载的图像保存到本地缓存。您不需要将图像存储在数组中。只需将图像的 URL 存储在数组中,Kingfisher 将自动加载缓存的图像。