-
Notifications
You must be signed in to change notification settings - Fork 485
Closed
Description
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
Labels
No labels