2016-09-25 78 views
0

我想從firebase存儲中下載圖像並將其加載到tableview中,但它不起作用。 我的downloadUrl是正確的,沒有錯誤。UITableViewCell圖像未加載

URLSession.shared.dataTask(with: downloadURL) { (data, response, error) in 
     if error != nil { 
      print(error?.localizedDescription) 
      return 
     } 
     DispatchQueue.main.async { 
      if let downloadedImage = UIImage(data: data!) { 
       cell.imageView?.image = downloadedImage 
      } 
     } 
    } 
+0

什麼不工作?你有錯誤嗎? 您是否嘗試過使用斷點來查看過程中失敗的內容?單元格是否爲空?圖像是否爲空?你能否詳細說明你所知道的*是否正確以及你認爲問題開始的地方。因此,我們可以開始工作了:) –

+0

單元格不爲空,圖像不是空的,我沒有錯誤。我想在我的tableview中顯示圖像,但圖像不顯示。 –

+0

嘗試在派發或其他事物中打印圖像,並查看是否可以打印它。 – Dravidian

回答

0

爲什麼不直接使用Firebase存儲的built in download mechanisms這個?

let httpsReference = FIRStorage.storage().referenceForURL('https://firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg') // your download URL 
httpsReference.dataWithMaxSize(1 * 1024 * 1024) { (data, error) -> Void in 
    if (error != nil) { 
    // Uh-oh, an error occurred! 
    } else { 
    // Data for "images/stars.jpg" is returned 
    let starsImage: UIImage! = UIImage(data: data!) 
    cell.imageView.image = starsImage // by default, callbacks are raised on the main thread so this will work 
    } 
}