【发布时间】:2019-09-21 09:34:50
【问题描述】:
当我将图像上传到 firebase 存储后,如何将图像保存到 firebase firestore?
当我将图像上传到 firebase 存储后,如何将图像保存到 firebase firestore?
我应该有带有标题和图片的帖子,但我不知道如何做图片部分。
我的代码如下所示......
import Foundation
import FirebaseFirestore
protocol DocumentSerializable {
init?(dictionary:[String:Any])
}
struct Post {
var name: String
var content: String
var downloadURL: String
var dictionary: [String:Any] {
return [
"name": name,
"content": content,
"imageDownloadURL": downloadURL
]
}
}
extension Post: DocumentSerializable {
init?(dictionary: [String : Any]) {
guard let name = dictionary["name"] as? String,
let content = dictionary["content"] as? String,
let downloadURL = dictionary["imageDownloadURL"] as? String else {return nil}
self.init(name: name, content: content, downloadURL: downloadURL)
}
}
import UIKit
import Firebase
import FirebaseStorage
class PostCell: UICollectionViewCell {
@IBOutlet weak var thumbnailImageView: UIImageView!
@IBOutlet weak var titleLabel: UILabel!
var post: Post! {
didSet {
self.updateUI()
}
}
func updateUI() {
// download image
if let imageDownloadURL = post?.downloadURL {
let imageStorageRef = Storage.storage().reference(forURL: imageDownloadURL)
imageStorageRef.getData(maxSize: 2 * 1024 * 1024) { [weak self] (data, error) in
if let error = error {
print("******** \(error)")
} else {
if let imageData = data {
let image = UIImage(data: imageData)
DispatchQueue.main.async {
self?.thumbnailImageView.image = image
}
}
}
}
}
}
}
【问题讨论】:
标签: swift firebase google-cloud-firestore