来自Firebase数据库的PhotoUrl显示在CoverFlow中

时间:2017-11-30 17:06:34

标签: 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!
}

1 个答案:

答案 0 :(得分:1)

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

所以,首先,你需要获得所有的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所必需的操作队列管理。如果您还不熟悉,请务必查看。

另外值得注意的是:很多人使用超级流行的第三方cocoapod SDWebImage来处理数据的下载。如果您要从Firebase数据库下载/分配到UIImageViews,可能需要阅读相关内容。