ios - Firebase 数据库中的 PhotoUrl 在 CoverFlow 中显示

标签 ios swift database firebase firebase-realtime-database

我正在创建一个应用程序,用户可以在其中发布图片并使用库“iCarousel”在 coverFlow 中滚动。 但是代码没有显示我数据库中的任何图片。

import UIKit
import Firebase

class HomeViewController: UIViewController, iCarouselDataSource, iCarouselDelegate {
    var imageArray : NSMutableArray = NSMutableArray()

    override func viewDidLoad() {
        super.viewDidLoad()

        let database = Database.database().reference()
        let tempImageRef = database.child("posts").child("photoUrl")
        imageArray = ["photo1", "photo2", "photo1", "photo2", "photo1"]

        carouselView.type = iCarouselType.coverFlow
        carouselView.reloadData()

        func numberOfItemsInCarousel(carousel: iCarousel) -> Int {
            return imageArray.count
        }

        func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {

            var imageView : UIImageView!

            if view == nil {
                imageView  = UIImageView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
                imageView.contentMode = .scaleAspectFit
            } else {
                imageView = view as! UIImageView
            }

            imageView.image = UIImage(named: "\(imageArray.object(at: index))")
            return imageView
        }
    }

    func numberOfItems(in carousel: iCarousel) -> Int {
        return imageArray.count
    }

    func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
        var imageView : UIImageView!

        if view == nil {
            imageView  = UIImageView(frame: CGRect(x: 0, y: 0, width: 250, height: 250))
            imageView.contentMode = .scaleAspectFit
        } else {
            imageView = view as! UIImageView
        }

        imageView.image = UIImage(named: "\(imageArray.object(at: index))")
        return imageView
    }

    @IBOutlet var carouselView: iCarousel!
    @IBOutlet weak var imageViewer: UIImageView!
}

最佳答案

我在您的代码中没有看到您从 Firebase 数据库中获取 photoUrls 的任何地方,也没有看到您试图在该 url 下载数据并将其转换为 UIImage 的任何地方。使用 UIImage(named: ... 初始化程序仅适用于 bundle 中的图像。

因此,首先,您需要获取所有 imageUrls。您有很多选择,但这是一个:

self.tempImageRef.observe(.value, with: { (snapshot) in
  //I'm just assuming your urls are an array of strings, so change this if that's not the case.
  if let urls = snapshot.value as? [String] {
    //Save the fetched urls to your imageArray
    self.imageArray = urls
  }
}

现在您有了 imageUrls,您可以在轮播中使用它们。同样,有很多选择,但这里有一个:

func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
  //Get a reference to the imageUrl at this index
  let imageUrl = self.imageArray[index]
  //Create a URL from the string
  let url = URL(string: imageUrl)!
  //Create a URLRequest and set the HTTP method to GET
  var urlRequest = URLRequest(url: url)
  urlRequest.httpMethod = "GET"
  //Create URLSession to handle the request
  let session = URLSession(configuration: .default)
  session.dataTask(with: URLRequest, completionHandler: { (data, response, error) in
    //Check to make sure you got data back, and that you can convert it to a usable UIImage
    if let imgData = data, let img = UIImage(data: imgData) {
      //Assign the image to your imageView
      imageView.image = img
    }
  }).resume()
}

这不包括您应该使用 Swift 执行的所有安全可选解包,也不包括避免锁定 UI 所必需的操作队列管理。如果您还不熟悉,请务必仔细研究。

还值得注意的是:许多人使用 super 流行的第三方 cocoapod SDWebImage 来处理数据的下载。如果您要从 Firebase 数据库向您的 UIImageViews 进行大量下载/分配,您可能想阅读这篇文章。

关于ios - Firebase 数据库中的 PhotoUrl 在 CoverFlow 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47578886/

相关文章:

ios - 在 iOS swift 中使用多点连接

swift - 在异步函数在 Swift 中完成执行后,在尾随闭包之外获取一个完全填充的数组

ios - SceneKit 的 SCNMatrix4 存储为列优先还是行优先?

ios - Passbook在附近时如何修改锁屏显示的内容

ios - 应用程序加载器 - 在错误的应用程序构建上推送构建

ios - 使用 Objective-C 的 HTTP 客户端上传图像

ios - 将容器中的 UICollectionView 单元居中

sql - 为什么在更改值时查询永远需要

database - 为什么基于列的数据库查询速度更快?

mysql - 在一张表中建模数据与使用两张表相比的优势