如何使用Swift 4中的SDWebImage缓存从firebase存储中获取的图像数组?

问题描述 投票:0回答:2

我以数据的形式从firebase存储中获取图像,我想使用SDWebImage在我的应用程序中缓存图像。请指导我如何实现它?

 for x in images_list{

                let storageRef = storage.child("images/\(x).jpg")
                storageRef.getData(maxSize: 1 * 1024 * 1024) { (data, error) in
                    if let error = error{
                        print(error.localizedDescription)
                        return
                    }
                    else{
                        imagesarray.append(UIImage(data: data!)!)
                    }
                    if let x = images_list.last{
                                cell.imageDemo.animationImages = imagesarray
                                cell.imageDemo.sd_setImage(with: storageRef) // Here I Want to cache the images of **imagesarray** that are being animated
                                cell.imageDemo.animationDuration = 2
                                cell.imageDemo.startAnimating()

                    }
                }


            }
swift caching firebase-storage sdwebimage
2个回答
1
投票

您正在问题中直接从firebase下载imageData。而不是使用getData你将需要使用downloadURL方法。像这样:

for x in image_list {

        let storageRef = storage.child("images/\(x).jpg")

        //then instead of downloading data download the corresponding URL
        storageRef.downloadURL { url, error in
            if let error = error {

            } else {

                //make your imageArray to hold URL rather then images directly
                imagesArray.append(url)
            }
        }

    }

    //////This is were you would use those url////////
    let url = imageArray.first!
    imageView.sd_setImage(with: url, completed: nil)

这里最重要的部分是下载URL而不是图像数据。每当您将图像URL设置为imageView时,SDWebImage库将对其进行缓存并显示(如果已缓存)


0
投票

我使用了downloadURL方法而不是getData方法来缓存图像数组。

var imagesarray = [URL]()
for x in images_list{
        let storageRef = storage.child("images/\(x).jpg")
        storageRef.downloadURL { (url, error) in
            if let error = error{
                print(error.localizedDescription)
            }
            else{
                imagesarray.append(url!)
            }
            if let x = images_list.last{
                cell.imageDemo.sd_setAnimationImages(with: imagesarray)
                cell.imageDemo.animationDuration = 2
                cell.imageDemo.startAnimating()
            }
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.