Firebase如何快速实现具有多个图像的发布功能 [英] Firebase how to implement post feature with multiple images in swift
问题描述
我想实现像instagram这样的功能,用户可以发布多张图片.我有tableviewcell,在tableviewcell上,有collectionviewcell来显示图像.
I would like to implement function like instagram that user can post with multiple images. I have tableviewcell and on the tableviewcell, there is collectionviewcell to display images.
这是我的Firebase结构
Here is my firebase structure
"Post": {
"uid": {
"Text": "some text",
"Date": "some date",
"images": {
autoID1: url1,
autoID2: url2
}
}
}
这是我的模特.我可以正确初始化吗?
Here is my model. Am I initializing properly?
import Foundation
import Firebase
import FirebaseDatabase
struct Post {
var Text: String!
var Date: String!
var images: String?
init(snapshot: FIRDataSnapshot){
for child in snapshot.children{
key = snapshot.key
ref = snapshot.ref
Text = (snapshot.value! as! NSDictionary)["Text"] as! String
Date = (snapshot.value! as! NSDictionary)["Date"] as? String
let snap = child as! DataSnapshot
images = snap.value as! String
}
}
}
然后,我想这样获取信息,以便最新帖子显示在顶部.但是问题在于用户不会每次都选择图像.有时,他们只会发布一些文本,在这种情况下,应用程序会崩溃.
then I would like to fetch like this so that latest post appear on top. but problem is that user don't pick images every time. sometimes, they only post some text, in that case app will crush.
var postsArray = [Post]()
func fetchPosts(){
dataBaseRef.child("Post").child(uid!).observe(.value, with: { (snapshot) in
var results = [Post]()
for post in snapshot.children {
let post = Post(snapshot: post as! FIRDataSnapshot)
results.append(post)
}
self.usersArray = results.sorted(by: { (u1, u2) -> Bool in
u1.Date > u2.Date
})
self.tableView.reloadData()
}) { (error) in
print(error.localizedDescription)
}
}
我的问题是如何创建最新帖子排在最前面的功能,如果未选择图像,则仅观察到文本和日期并显示在tableviewcell上?
My question is how to create feature that latest post comes on top and if no images picked, only text and date observed and display on the tableviewcell?
此外,当要在集合视图上显示多个图像时,我应该如何配置该单元格?我实现了文本和日期,但是我在图像上苦苦挣扎.
Also, how should I configure the cell when it comes to display multiple images on collection view? I implemented Text and Date but I'm struggling with images.
func configureCell(post: Post){
self.TextLabel.text = post.Text
self.DateLabel.text = post.Date
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "postsCell", for: indexPath) as! PostsTableViewCell
cell.configureCell(post: postsArray[indexPath.row])
return cell
}
//how to configure cell?
import UIKit
import Firebase
class PostCollectionViewCell: UICollectionViewCell {
@IBOutlet var imageView: UIImageView!
var storageRef: Storage {
return Storage.storage()
}
}
提前谢谢!
推荐答案
这是Firebase的 DataSnapshot
(又名 FIRDataSnapshot
)的扩展,它将有助于从快照为您班上最方便的类型.在特定的日期情况下,我假设您使用 Firebase.ServerValue
将它们存储为毫秒.
This is an extension to Firebase's DataSnapshot
(aka. FIRDataSnapshot
) that will help converting values from a snapshot into the most convenient types for your classes. In the specific case of the date, I am assuming you are storing them as milliseconds using Firebase.ServerValue
.
extension DataSnapshot {
func child(_ pathString: String) -> DataSnapshot {
return childSnapshot(forPath: pathString)
}
var snapshots: [DataSnapshot] {
return children.allObjects.flatMap({ $0 as? DataSnapshot })
}
var date: Date? { return double.flatMap({ $0 / 1000 }) }
var double: Double? { return value as? Double }
var string: String? { return value as? String }
var url: URL? { return string.flatMap({ URL(string: $0) }) }
var urls: [URL] { return snapshots.flatMap({ $0.url }) }
}
您现在可以使用快照初始化 Post
,并方便地将属性强制转换为正确的类型,如果属性不存在,则不会崩溃.这样,您在为单元格配置数据时就无需考虑什么了.
You can now initialise a Post
with a snapshot and have the properties conveniently casted to the right type without crashing if they're not there. This way you have less to think about when you are configuring cells with the data.
struct Post {
// MARK: Properties
let snapshot: DataSnapshot
var created: Date!
var images: [URL]?
var text: String!
// MARK: Lifecycle
init(snapshot: DataSnapshot) {
self.snapshot = snapshot
self.created = snapshot.child("created").date
self.images = snapshot.child("images").urls
self.text = snapshot.child("text").string
}
}
如果存储图像的 downloadURL
而不是存储引用,则
配置单元格以显示图像会容易得多.当您将帖子传递到表格视图单元格时,只需要告诉集合视图有多少个URL,然后为indexPath中的每个单元格在帖子的images数组中配置相应的URL即可.
Configuring the cell to show an image will be a lot easier if you store the downloadURL
of the images instead of a storage reference. When you pass the post to the table view cell, it just needs to tell a collection view how many URLs it has – then for each cell at an indexPath, you configure it with the corresponding URL in the post's images array.
这篇关于Firebase如何快速实现具有多个图像的发布功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!