Skip to content

Feature request: Add functionality to preload image using SDWebImage #657

@otusweb

Description

@otusweb

The current SDK allows to load an image straight to an UIImageView. In my case i'd like to preload the image before setting in the imageView as I have to calculate some layout that requires having the image locally.

here is a swift version of the method I created to do just that:

class func imageForReference(_ reference: StorageReference, completion: @escaping (UIImage?, Error?) -> Void) {
        // Query cache for image before trying to download
        let cache = SDImageCache.shared()
        let key = reference.fullPath
        var cached = cache.imageFromMemoryCache(forKey: key)
        if cached != nil {
            completion(cached, nil)
            return
        }

        cached = cache.imageFromDiskCache(forKey: key)
        if cached != nil {
            completion(cached, nil)

            return
        }

        // If nothing was found in cache, download the image from Firebase Storage
        let filePath = FileManager.default.temporaryDirectory.appendingPathComponent(key)
        reference.write(toFile: filePath) { (file: URL?, error: Error?) in
            DispatchQueue.main.async {
                guard error == nil, let file = file else {
                    completion(nil, error)
                    return
                }
                let image = UIImage(contentsOfFile: file.absoluteString)
                cache.store(image, forKey: key, completion: nil)

                completion(image, nil)
            }
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions