H. *_*jir 5 firebase swift google-cloud-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
}
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
您不\xe2\x80\x99t 将图像保存到firestore 中,而是将图像url 保存到firestore 中。将图像保存到存储后,您必须获取该图像的 url,然后将该 url 保存到 firestore。只需谷歌搜索这些步骤或查看文档以获取更多信息
\n| 归档时间: |
|
| 查看次数: |
7665 次 |
| 最近记录: |