我正在尝试学习firebase.firebase上有两个文件夹
1)VDBackgroundFrames / 2)VDFrames /
在两个文件夹中,我们都有4张图像-VDBG2.png,VDBG3.png,VDBG4.png,VDBG5.png。
我可以使用以下代码从firebase一次访问一张图像:-
func firebaseSetUp(){
let store = Storage.storage()
let storeRef = store.reference()
let userProfilesRef = storeRef.child("VDBackgroundFrames/VDBG11.jpg")
userProfilesRef.downloadURL { (url,error) in
if error != nil {
print("error?.localizedDescription",error?.localizedDescription)
return
}else{
print("url",url!)
}
}
}
// ==========更新代码==== //
func firebaseSetUp(){
let store = Storage.storage()
let storeRef = store.reference()
let userProfilesRef = storeRef.child("VDBackgroundFrames/")
userProfilesRef.observe(.childAdded, with: { [weak self] (snapshot) -> Void in
guard let strongSelf = self else { return }
//Logic to extract urls...
}, changeHandler: (StorageReference, NSKeyValueObservedChange<Value>) -> Void)
}
我得到的输出如下:-
URL
我实际上想分别从文件夹VDBackgroundFrames和VDFrames一起访问所有4个图像。请提出可能的实现方法。任何建议或指导都是可取的。 预先感谢。
答案 0 :(得分:0)
只需直接访问根文件夹而不是直接访问子文件夹,这样您将获得该文件夹中的所有节点/图像,如下所示:
func firebaseSetUp(){
let store = Storage.storage()
let storeRef = store.reference()
let userProfilesRef = storeRef.child("VDBackgroundFrames/")
userProfilesRef.observe(.childAdded, with: { [weak self] (snapshot) -> Void in
guard let strongSelf = self else { return }
//Logic to extract urls...
}
}
答案 1 :(得分:0)
DownloadURL一次只包含一个字符串。如果您想将文件夹中的所有文件显示在像我这样的表格视图中,请使用完整代码:
import UIKit import Firebase
My very First View Controller-
class FolderList: UIViewController {
var folderList: [StorageReference]?
lazy var storage = Storage.storage()
@IBOutlet weak var tableView : UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.storage.reference().child("TestFolder").listAll(completion: {
(result,error) in
print("result is \(result)")
self.folderList = result.items
DispatchQueue.main.async {
self.tableView.reloadData()
}
})
} }
extension FolderList : UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return folderList?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "FolderListCell", for:
indexPath) as? FolderListCell else {return UITableViewCell()}
cell.itemName.text = folderList?[indexPath.row].name
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 64.0
} }
extension FolderList : UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
guard let downloadVC = storyBoard.instantiateViewController(withIdentifier:
"DownloadedItemView") as? DownloadedItemView else {
return
}
downloadVC.storageRef = folderList?[indexPath.row]
self.navigationController?.pushViewController(downloadVC, animated: true)
}
}
And here is you DownloadedItemView, which will open the images you selected from the list in a view:
import UIKit
import WebKit
import Firebase
class DownloadedItemView: UIViewController {
@IBOutlet weak var webView : WKWebView!
var downloadItemURL : String?
var storageRef : StorageReference?
override func viewDidLoad() {
super.viewDidLoad()
storageRef?.downloadURL(completion: {(downloadURL,error) in
print("url is \(downloadURL)")
DispatchQueue.main.async {
guard let url = downloadURL else {return}
let urlrequest = URLRequest(url: url)
self.webView.load(urlrequest)
}
})
}
}
Your each cell:
class FolderListCell: UITableViewCell {
@IBOutlet weak var itemName : UILabel!
}