[英]How to retrieve image from firebase storage Swift 4 ios
I am trying to retrieve an image from firebase storage but the image that I retrieve always is nil
for some reason. 我正在尝试从Firebase存储中检索图像,但是由于某种原因,我检索到的图像始终为
nil
。
var ref: DatabaseReference!
var storageRef: StorageReference!
var hallData = [Hall]()
override func viewDidLoad() {
let refHandle = Database.database().reference().child("hallData").observe(DataEventType.value, with: { (snapshot) in
let postDict = snapshot.value as? [String : AnyObject] ?? [:]
let values = Array(postDict.values)
//print(values)
let valueDict = values as! [[String:Any]]
for i in valueDict
{
var name = i["name"] as! String
var address = i["address"] as! String
var capacity = i["capacity"] as! String
var decorations = i["decorations"] as! String
var highPrice = i["highPrice"] as! String
var lowPrice = i["lowPrice"] as! String
var catering = i["catering"] as! String
var email = i["email"] as! String
self.storageRef = Storage.storage().reference().child("images").child(email)
var image: UIImage!
// Download in memory with a maximum allowed size of 1MB (1 * 1024 * 1024 bytes)
self.storageRef.getData(maxSize: 1 * 1024 * 1024) { data, error in
if let error = error {
print("PLASESEE")
print(error.localizedDescription)
// Uh-oh, an error occurred!
} else {
// Data for "images/island.jpg" is returned
let image = UIImage(data: data!)
}
}
print(image)
self.hallData.append(Hall(name2: name, capacity2: capacity, lowPrice2: lowPrice, highPrice2: highPrice, catering2: catering,decorations2: decorations, address2:address, image2: image, email2: email))
}
})
}
I dont understand what I am doing wrong, I followed the api on firebase storage, checked out a lot of tutorials but I keep getting nil
我不明白自己在做什么错,我遵循了Firebase存储上的api,签出了很多教程,但我一直
nil
The issue is that you're attempting to work with the image var outside the getData closure. 问题是您正在尝试使用getData闭包之外的image var。
self.storageRef.getData(maxSize: 1 * 1024 * 1024) { data, error in
if let error = error {
print("PLASESEE")
print(error.localizedDescription)
// Uh-oh, an error occurred!
} else {
// Data for "images/island.jpg" is returned
let image = UIImage(data: data!)
} <- closure ends here and image is only valid above this
}
print(image) <- image may not be populated at this point
self.hallData.append... image
}
That closure is asynchronous and the call to self.hallData.append...
will occur way before the image var is populated within the closure. 该关闭是异步的,对
self.hallData.append...
的调用将在将图像var填充到关闭中之前发生。 Code is much faster than the internet 代码比互联网快得多
Move that statement inside the closure, right after the let image =
and it should work. 在
let image =
之后紧跟在闭包内移动该语句,它应该可以工作。
self.storageRef.getData(maxSize: 1 * 1024 * 1024) { data, error in
if let error = error {
print("An error occurred in downloading the image")
print(error.localizedDescription)
} else {
let image = UIImage(data: data!)
self.hallData.append... image
//reload your tableView or UI as self.hallData is now valid
}
}
If you do it this way, the preceeding var image: UIImage!
如果您采用这种方式,则前面的
var image: UIImage!
can be removed as it doesn't have a function. 可以删除,因为它没有功能。 Otherwise, remove the let before let image = within the closure.
否则,请在闭包内删除let before let image =。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.