【问题标题】:Saving image to Cloud firestore将图像保存到 Cloud Firestore
【发布时间】: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


    【解决方案1】:

    您不会将图像保存到 firestore,而是将图像 url。将图像保存到存储中后,您必须获取该图像的 url,然后将 url 保存到 firestore。只需谷歌这些步骤或查看文档了解更多信息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-14
      • 2021-08-22
      • 1970-01-01
      • 1970-01-01
      • 2019-07-27
      • 2019-10-27
      • 1970-01-01
      • 2021-03-20
      相关资源
      最近更新 更多