我使用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
]
}
}
答案 0 :(得分:2)
使用FirebaseUI / Storage:添加到Podfile
:
pod 'FirebaseUI/Storage'
然后,在您的代码中,显示和缓存图像:
import FirebaseStorageUI
let imgRef = Storage.storage().reference().child("images/\(blah)")
myImageView.sd_setImage(with: imgRef))
就是这样!如果使用相同的引用,图像将在后台获取,并在下次缓存。
答案 1 :(得分:0)
Kingfisher非常感谢,但是我现在有一些问题:它现在很慢(参见单独的主题)
Kingfisher with Firebase storage slow to display images - swift 3
哦,我通过在结构中包含空白imageURL来修复快照,然后在将图像保存到FirebaseStorage时写入快照。这意味着结构在带有URL
的快照中返回