gpt4 book ai didi

ios - 火力地堡 iOS : Download image for TableView - Best Practice

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:24:59 24 4
gpt4 key购买 nike

我遵循了 Ray Wenderlich (Link) 的 Firebase 教程,并采用了他使用观察方法的快照来初始化对象(在我的情况下是“位置”类型)的方法:

上课地点:

init(snapshot: FIRDataSnapshot) {
identifier = snapshot.key
let snapshotValue = snapshot.value as! [String : AnyObject]
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
}

位置 View Controller :

databaseHandle = locationsRef?.queryOrdered(byChild: "name").observe(.value, with: { (snapshot) in

var newLocations:[Location] = []

for loc in snapshot.children {
let location = Location(snapshot: loc as! FIRDataSnapshot)

newLocations.append(location)
}

self.locations = newLocations
self.tableView.reloadData()
})

这真的很有效,但现在我正在尝试加载存储在存储引用“avatarPath”下的图像。我的尝试成功了,但图像需要很长时间才能加载。有没有更好的方式/地方来加载这些图像?

我的尝试 1:

databaseHandle = locationsRef?.queryOrdered(byChild: "name").observe(.value, with: { (snapshot) in

var newLocations:[Location] = []

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()
}
})
}
})

我的第二次尝试(在 Location 类中):

init(snapshot: FIRDataSnapshot) {
identifier = snapshot.key
let snapshotValue = snapshot.value as! [String : AnyObject]
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!)!
}
})
}

提前致谢!

妮可

最佳答案

实现此目的的最佳方法是在单元格函数的加载过程中进行异步加载。我的意思是:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
DispatchQueue.main.async {
let imagesStorageRef = FIRStorage.storage().reference().child(self.locations[indexPath.row].avatarPath)
imagesStorageRef.data(withMaxSize: 1*1024*1024, completion: { (data, error) in
if let error = error {
print(error.localizedDescription)
} else {
locations[indexPath.row].avatarImage = UIImage(data: data!)!
tableView.reloadRows(at indexPaths: [indexPath], with animation: .none)
}
})
}

关于ios - 火力地堡 iOS : Download image for TableView - Best Practice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43228922/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com