Swift 3 中的 Firebase 存储图像持久性

Firebase Storage image persistence in Swift 3

我使用 this tutorial 将数据添加到 Firebase。它工作得很好。由于快照和持久性,它也可以离线工作。

我还从 this link 添加图像到 Firebase 存储。这也有效,但不适用于持久性。我试图从数据(第一个教程)中复制快照来执行此操作,但据我所知,FIRStorageReference 没有快照选项。我该怎么做?

import Foundation
import Firebase
import FirebaseStorage

struct MenuItemImage {

let image: NSData
let ref: FIRStorageReference?
let key: String



init(image: NSData, key: String = "") {
    self.key = key
    self.image = image
    self.ref = nil

}

init(snapshot: FIRDataSnapshot) { // this should be something like FIRStoreSnapshot
    key = snapshot.key
    let snapshotValue = snapshot.value as! [String: AnyObject]
    image = snapshotValue["image"] as! NSData
    ref = snapshot.ref // errors as cannot assign value of tupe FIRDatabaseReference to type FIRStorageReference
}

func toAnyObject() -> Any {
    return [
        "image": image
    ]
}

}

Kingfisher 工作得很好谢谢,但是我确实有一些问题,它现在很慢(参见单独的线程)

哦,我通过在结构中包含空白图像URL 修复了快照,然后在将图像保存到 FirebaseStorage 时写入它。这意味着该结构在 URL

的快照中返回

使用FirebaseUI/Storage:添加到您的Podfile:

pod 'FirebaseUI/Storage'

然后,在您的代码中,要显示和缓存图像,请执行以下操作:

import FirebaseStorageUI

let imgRef = Storage.storage().reference().child("images/\(blah)")
myImageView.sd_setImage(with: imgRef))

就是这样!如果使用相同的引用,将在后台获取图像,并缓存以供下次使用。