如何根据需要的图像类别从Firebase存储中检索图像

时间:2019-05-08 12:01:23

标签: swift xcode firebase firebase-storage

如何从Firebase存储器中检索图像,在该存储器中,我已经根据“墙纸应用”的类别手动创建了文件夹,并且根据墙纸的类别创建了“项目”列表。我不了解如何从Firebase的存储中根据“项目”(位于下面的代码中)检索图像并进行相应显示。...

我只想按照下面的代码中指定的“项目”类别存储所有图像,并在应用程序中显示。

//未格式化 导入UIKit 导入GlidingCollection 进口FirebaseStorage 导入Firebase

//formatted properly
    import UIKit
    import GlidingCollection
    import FirebaseStorage
    import Firebase
// end of properly formatted code

ViewController类:UIViewController {

@IBOutlet var glidingView: GlidingCollection!
fileprivate var collectionView: UICollectionView!
fileprivate var items = ["riches", "animals", "nature", "architecture","toys"]
fileprivate var images: [[UIImage?]] = []

override func viewDidLoad() {
    super.viewDidLoad()
    setup()
}
func setup() {
    setupGlidingCollectionView()
    loadImages()
}

func setupGlidingCollectionView() {
    glidingView.dataSource = self

    let nib = UINib(nibName: "CollectionCell", bundle: nil)
    collectionView = glidingView.collectionView
    collectionView.register(nib, forCellWithReuseIdentifier: "Cell")
    collectionView.delegate = self
    collectionView.dataSource = self
    collectionView.backgroundColor = glidingView.backgroundColor
}
func loadImages() {
    let storage = Storage.storage()
    let storageRef = storage.reference()
    let starsRef = storageRef.child("Animals")
    starsRef.downloadURL { url, error in
        if let error = error {
            print("not there")
        } else {
            for item in self.items {
                let imageURLs = FileManager.default.fileUrls(for: "jpeg", fileName: item)
                var images: [UIImage?] = []
                for url in imageURLs {
                    guard let data = try? Data(contentsOf: url) else { continue }
                    let image = UIImage(data: data)
                    images.append(image)
                }
                self.images.append(images)
            }
        }
    }
}

}

// MARK:-设置

// MARK:-CollectionView 扩展ViewController:UICollectionViewDataSource,UICollectionViewDelegate {     func collectionView(_ collectionView:UICollectionView,numberOfItemsInSection部分:Int)-> Int {         返回images.count     }     func collectionView(_ collectionView:UICollectionView,cellForItemAt indexPath:IndexPath)-> UICollectionViewCell {         守卫让细胞= collectionView.dequeueReusableCell(withReuseIdentifier:“ Cell”,for:indexPath)为? CollectionCell else {返回UICollectionViewCell()}         让部分= glidingView.expandedItemIndex         让图片=图片[section] [indexPath.row]         cell.imageView.image =图片         cell.contentView.clipsToBounds = true

    let layer = cell.layer
    let config = GlidingConfig.shared
    layer.shadowOffset = config.cardShadowOffset
    layer.shadowColor = config.cardShadowColor.cgColor
    layer.shadowOpacity = config.cardShadowOpacity
    layer.shadowRadius = config.cardShadowRadius
    layer.shouldRasterize = true
    layer.rasterizationScale = UIScreen.main.scale
    return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let section = glidingView.expandedItemIndex
    let item = indexPath.item
    print("Selected item #\(item) in section #\(section)")
}

}

// MARK:-滑行收集 扩展ViewController:GlidingCollectionDatasource {

func numberOfItems(in collection: GlidingCollection) -> Int {
    return items.count
}

func glidingCollection(_ collection: GlidingCollection, itemAtIndex index: Int) -> String {
    return "– " + items[index]
}

}

另一个链接到集合视图单元格的快捷文件中。“ CollectionCell.xib”

导入UIKit

CollectionCell类:UICollectionViewCell {     @IBOutlet弱var imageView:UIImageView!

}

我希望对所有图像进行分类并根据我的应用程序中显示的项目进行操作。......

0 个答案:

没有答案