【问题标题】:Swift set image on TableView from retrieved firebase data从检索到的 firebase 数据在 TableView 上快速设置图像
【发布时间】:2017-08-11 20:24:16
【问题描述】:

所以我构造检索数据的方式是;

结构框架图片 { 让 imagedB :数据! 让 pricedB :字符串! }

我已成功检索数据,但我不知道如何在 tableView 上设置图像?我已经设法用标签弄清楚了......但不是用图片?

这是表格中单元格的代码;

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


    let cell: RowCellTableView = myTableView.dequeueReusableCell(withIdentifier: "cell") as! RowCellTableView

     // sets label
    cell.priceView.text = framePics[indexPath.row].pricedB

    // set image on UIimageView???
    --- I dont know how to add this? I need somehow to get the list of only the images???  I believe if I had the array variable like; imageFiles[] I could have done;
imageFiles[indexPath.row].getDataInBackground... But in my case I use struct so how am I supposed to do this in this case?

RowCellTableView 只包含;

  @IBOutlet weak var imageView: UIImageView!
  @IBOutlet weak var priceView: UILabel!

【问题讨论】:

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


    【解决方案1】:

    在你的结构中你有:

    struct framePic { 
       let imagedB : Data! 
       let pricedB : String! 
    }
    

    尝试像这样将 imagedB 转换为 UIImage:

    var imageUIImage: UIImage = UIImage(data: imageData)
    

    所以你的实现会是这样的:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    
        let cell: RowCellTableView = myTableView.dequeueReusableCell(withIdentifier: "cell") as! RowCellTableView
    
         // sets label
        cell.priceView.text = framePics[indexPath.row].pricedB
    
        // convert imageData to UIImage
        var imageUIImage: UIImage = UIImage(data: framePics[indexPath.row].imagedB)
    
        // apply image to UIImageView
        cell.imageView.image = imageUIImage
    
        return cell
    }
    

    【讨论】:

      【解决方案2】:
      cell.imageView.image = someImage
      

      【讨论】:

      • 实际上,由于您是从 Firebase 获取此数据,因此您不一定要拉取图像,而是拉取图像的 URL。如果您使用允许您将 URL 传递给 imageView 的框架,您可以这样做,并且只传递 URL
      • 是的,我获取了一个 URL,那么什么样的框架支持这个呢?输入 URL 并获取图像?嗯...
      • @StrawHat 这肯定对你有用。它是一个很棒的 iOS 库列表,您可以通过 Cocoapods 轻松地将其集成到您的项目中。 github.com/vsouza/awesome-ios#image
      • @StrawHat 我会推荐 SDWebImage,但是看看哪个套件最适合您
      • 你能具体告诉我一个吗?我在数据库中保存一个 URL 并将图像保存在存储中......
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-14
      • 2018-01-16
      相关资源
      最近更新 更多