我想从firebase存储下载图像并将其加载到tableview中,但它不工作。我的downloadUrl
是正确的,没有错误。
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
}
}
}
为什么不直接使用Firebase Storage内置的下载机制呢?
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
}
}