【问题标题】:PhotoUrl from Firebase Database display in CoverFlow来自 Firebase 数据库的 PhotoUrl 显示在 CoverFlow 中
【发布时间】:2017-11-30 17:06:34
【问题描述】:

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

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!
}

【问题讨论】:

  • 为什么viewDidLoad里面有carousel delegate和data source函数?那是行不通的。

标签: ios swift database firebase firebase-realtime-database


【解决方案1】:

我在您的代码中没有看到您从 Firebase 数据库中获取 photoUrls 的任何地方,也没有看到您尝试在该 url 下载数据并将其转换为 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,您可能需要阅读此内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-09
    • 1970-01-01
    • 1970-01-01
    • 2020-07-23
    • 2021-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多