ios - Firebase iOS : Download image for TableView - Best Practice
<p><p>我遵循了 Ray Wenderlich (<a href="https://www.raywenderlich.com/139322/firebase-tutorial-getting-started-2" rel="noreferrer noopener nofollow">Link</a>) 的 Firebase 教程,并采用了他的方法,使用来自观察方法的快照初始化对象(在我的情况下为“位置”类型):</p>
<p><strong>上课地点:</strong></p>
<pre><code>init(snapshot: FIRDataSnapshot) {
identifier = snapshot.key
let snapshotValue = snapshot.value as!
type = snapshotValue["type"] as! String
name = snapshotValue["name"] as! String
address = snapshotValue["address"] as! String
latitude = Double(snapshotValue["latitude"] as! String)!
longitude = Double(snapshotValue["longitude"] as! String)!
avatarPath = snapshotValue["avatarPath"] as! String
ref = snapshot.ref
}
</code></pre>
<p><strong>LocationsViewController:</strong></p>
<pre><code>databaseHandle = locationsRef?.queryOrdered(byChild: "name").observe(.value, with: { (snapshot) in
var newLocations: = []
for loc in snapshot.children {
let location = Location(snapshot: loc as! FIRDataSnapshot)
newLocations.append(location)
}
self.locations = newLocations
self.tableView.reloadData()
})
</code></pre>
<p>这确实像一个魅力,但现在我正在尝试加载存储在存储引用“avatarPath”下的图像。
我的尝试奏效了,但图像需要很长时间才能加载。有没有更好的方法/地方来加载这些图像? </p>
<p><strong>我的尝试 1:</strong></p>
<pre><code>databaseHandle = locationsRef?.queryOrdered(byChild: "name").observe(.value, with: { (snapshot) in
var newLocations: = []
for loc in snapshot.children {
let location = Location(snapshot: loc as! FIRDataSnapshot)
newLocations.append(location)
}
self.locations = newLocations
self.tableView.reloadData()
//Load images
for loc in self.locations {
let imagesStorageRef = FIRStorage.storage().reference().child(loc.avatarPath)
imagesStorageRef.data(withMaxSize: 1*1024*1024, completion: { (data, error) in
if let error = error {
print(error.localizedDescription)
} else {
loc.avatarImage = UIImage(data: data!)!
self.tableView.reloadData()
}
})
}
})
</code></pre>
<p><strong>我的第二次尝试(在 Location 类中):</strong></p>
<pre><code>init(snapshot: FIRDataSnapshot) {
identifier = snapshot.key
let snapshotValue = snapshot.value as!
type = snapshotValue["type"] as! String
name = snapshotValue["name"] as! String
address = snapshotValue["address"] as! String
latitude = Double(snapshotValue["latitude"] as! String)!
longitude = Double(snapshotValue["longitude"] as! String)!
avatarPath = snapshotValue["avatarPath"] as! String
ref = snapshot.ref
super.init()
downloadImage()
}
func downloadImage() {
let imagesStorageRef = FIRStorage.storage().reference().child(self.avatarPath)
imagesStorageRef.data(withMaxSize: 1*1024*1024, completion: { (data, error) in
if let error = error {
print(error.localizedDescription)
} else {
self.avatarImage = UIImage(data: data!)!
}
})
}
</code></pre>
<p>提前谢谢你!</p>
<p>尼哥</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>实现这一点的最佳方法是在单元格函数的加载中异步加载。我的意思是:</p>
<pre><code>func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
DispatchQueue.main.async {
let imagesStorageRef = FIRStorage.storage().reference().child(self.locations.avatarPath)
imagesStorageRef.data(withMaxSize: 1*1024*1024, completion: { (data, error) in
if let error = error {
print(error.localizedDescription)
} else {
locations.avatarImage = UIImage(data: data!)!
tableView.reloadRows(at indexPaths: , with animation: .none)
}
})
}
</code></pre>
<p>}</p></p>
<p style="font-size: 20px;">关于ios - Firebase iOS : Download image for TableView - Best Practice,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/43228922/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/43228922/
</a>
</p>
页:
[1]