2017-04-19 93 views
-1

我是swift平臺的新用戶。我試圖將圖片安裝到Firebase存儲。然後從數據庫檢索到我自己的應用程序。我在互聯網上發現了一些代碼並相互關聯。 圖像不在屏幕上打印。 如何打印圖像。如何從Firebase存儲中檢索圖片

這裏是我的代碼:

let ref = FIRDatabase.database().reference() 

     ref.child("images").queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in 

      let images = snapshot.value as! [String : AnyObject] 
      self.places.removeAll() 
      for (_, value) in images { 
         let userToShow = historicalPlace() 
         if let img = value["place1"] as? String{ 

          userToShow.historyImage = img 

          self.places.append(userToShow) 

       } 
      } 
      self.historyTableView.reloadData() 
     }) 
     ref.removeAllObservers() 

    } 

    func numberOfSections(in tableView: UITableView) -> Int { 
     return 1 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = historyTableView.dequeueReusableCell(withIdentifier: "historyCell", for: indexPath) as! historyCell 


     cell.historyImage.downloadImage(from: places[indexPath.row].historyImage!) 


     return cell 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return places.count 
    } 

} 


extension UIImageView { 

    func downloadImage(from imgURL: String!) { 
     let url = URLRequest(url: URL(string: imgURL)!) 

     let task = URLSession.shared.dataTask(with: url) { 
      (data, response, error) in 

      if error != nil { 
       print(error!) 
       return 
      } 

      DispatchQueue.main.async { 
       self.image = UIImage(data: data!) 
      } 

     } 

     task.resume() 
    } 

} 

,這是我的數據庫:

Database

我把我的讀取和寫入正確的。

回答

0

我看看FirebaseUI,這與SDWebImage的集成,所以你可以這樣做:

// Reference to an image file in Cloud Storage 
let reference: FIRStorageReference = ...; 

// UIImageView in your ViewController 
var imageView: UIImageView = ...; 

// Load the image using SDWebImage 
imageView.sd_setImageWithStorageReference(reference, placeholderImage: placeholderImage) 

https://github.com/firebase/FirebaseUI-iOS/tree/master/FirebaseStorageUI

相關問題