【问题标题】:How to cache an array of Images fetched from firebase Storage using SDWebImage in Swift 4?如何在 Swift 4 中使用 SDWebImage 缓存从 firebase 存储中获取的图像数组?
【发布时间】:2019-02-19 00:38:43
【问题描述】:

我正在以数据的形式从 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


    【解决方案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 库都会缓存它并在已缓存时显示

    【讨论】:

    • 感谢您的帮助,但我仍然不明白如何缓存图像数组。我想到了一个解决方案,您能否建议它是否正确?
    • 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.sd_ } } }
    • 您的代码应该可以工作,一旦您使用此库将图像设置为 UIImageView,它应该会自动下载并缓存图像。所以下次它会加载得更快。您可以通过进入设备/模拟器的 Documents 目录来检查这一点。您的图像应该有一大堆缓存文件
    • 感谢兄弟的回复,这真的很有帮助,图像正在加载和动画,但显示速度非常快。我现在需要更改动画的持续时间
    • 您可以将imageView.animationDuration 设置为您想要的值
    【解决方案2】:

    我使用的是 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()
                }
            }
        }
    

    【讨论】:

    • 这应该可以正常工作。图像之间的动画延迟了 2。
    • 感谢兄弟的帮助。帮助很大
    猜你喜欢
    • 2016-03-03
    • 1970-01-01
    • 2014-04-15
    • 2013-01-31
    • 2021-02-14
    • 1970-01-01
    • 2012-10-13
    • 2023-03-22
    • 2017-12-29
    相关资源
    最近更新 更多