firebase 将映像保存到Cloud firestore

eyh26e7m  于 2023-03-19  发布在  其他
关注(0)|答案(1)|浏览(131)

当我将映像上传到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
                        }
                    }
                }
                
            }
        }
    }
    
    
}
w6lpcovy

w6lpcovy1#

你不保存图像到firestore,但图像的网址。当你保存图像到存储,你必须得到该图像的网址,然后保存到firestore。谷歌这些步骤或查看文档的更多信息

相关问题