从Firebase中检索图片 [英] Retrieving Image From Firebase
问题描述
let dbRef = FIRDatabase.database 应用程序运行成功,但图像没有显示在表格单元格中。 ().reference()。child(restaurants / restaurantImage)
dbRef.observe(.childAdded,其中:{(snapshot)in
let downloadURL = snapshot.value as!String
让存储参考= FIRStorage.storage()。参考(forURL:downloadURL)
storageRef.data(withMaxSize:10 * 1024 * 1024){(数据,错误) - >无效
let pic = UIImage (data:data!)
self.picArray.append(pic!)
self.tableViewHongKong.reloadData()
})
self.tableViewHongKong.reloadData()
在完成处理程序中。使用当前的代码,您可以在异步函数完成之前重新载入表。 除非您100%确定数据将实际返回,否则不应该强制展开。 UIImage初始化器将成功。
$ b $ pre $ let $ dbRef = FIRDatabase.database()。reference()。child(restaurants / restaurantImage )
dbRef.observe(.childAdded,其中:{(snapshot)in
let downloadURL = snapshot.value as!String
let storageRef = FIRStorage.storage()。reference(forURL:downloadURL )
storageRef.data(withMaxSize:10 * 1024 * 1024){(data,error) - >无效
guard让imageData = data,让pic = UIImage(data:imageData)else {return }
self.picArray.append(pic)
self.tableViewHongKong.reloadData()
}
})
The app run successfully , but the image did not show up in the table cell.
let dbRef = FIRDatabase.database().reference().child("restaurants/restaurantImage")
dbRef.observe(.childAdded, with: {(snapshot) in
let downloadURL = snapshot.value as! String
let storageRef = FIRStorage.storage().reference(forURL: downloadURL)
storageRef.data(withMaxSize: 10 * 1024 * 1024) { (data, error) -> Void in
let pic = UIImage(data: data!)
self.picArray.append(pic!)
}
self.tableViewHongKong.reloadData()
})
You should move self.tableViewHongKong.reloadData()
inside your completion handler. With your current code you reload the table before the asynchronous function would finish.
You shouldn't do force unwrapping unless you are 100% sure that data will actually return and that the UIImage initializer will succeed.
let dbRef = FIRDatabase.database().reference().child("restaurants/restaurantImage")
dbRef.observe(.childAdded, with: {(snapshot) in
let downloadURL = snapshot.value as! String
let storageRef = FIRStorage.storage().reference(forURL: downloadURL)
storageRef.data(withMaxSize: 10 * 1024 * 1024) { (data, error) -> Void in
guard let imageData = data, let pic = UIImage(data: imageData) else { return }
self.picArray.append(pic)
self.tableViewHongKong.reloadData()
}
})
这篇关于从Firebase中检索图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!