请帮助我。说明如何在单元格中设置图像。在我的数据库中,我有:标题,描述和imageURL(Firebase Storage的URL)。你能给我写代码并解释一下吗?
class TrainingProgram
{
var description = ""
var title = ""
var imageURL = ""
init(description: String, title: String, imageURL: String) {
self.description = description
self.title = title
self.imageURL = imageURL
}
函数从Firebase获取数据。
func fetchPrograms() {
Database.database().reference().child("programs").observe(.childAdded) { (snapshot) in
if let dict = snapshot.value as? [String: AnyObject] {
let newTitle = dict["title"] as! String
let newDescription = dict["description"] as! String
let newImageURL = dict["imageURL"] as! String
let trainingCell = TrainingProgram(description: newDescription, title: newTitle, imageURL: newImageURL)
self.trainingPrograms.append(trainingCell)
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}
}
}
这就是我为单元格设置标题和描述的方式。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TrainingProgramCollectionViewCell", for: indexPath) as! TrainingProgramCollectionViewCell
//cell.featuredImageView.setImage(from: indexPath.item)
cell.titleLabel.text = trainingPrograms[indexPath.item].title
cell.descriptionLabel.text = trainingPrograms[indexPath.item].description
我需要在函数中编写什么以获取数据以获取图像以及如何在单元格中设置图像
答案 0 :(得分:1)
通过此链接https://github.com/SDWebImage
安装SDWebImage pod在您的CollectioView cellForRow
方法中
var images_list = [String]()
var imagesArray = [URL]()
images_list.append(itemModel[indexPath.row]. imageURL)
let storage = Storage.storage().reference()
for x in images_list{
print(x)
let storageRef = storage.child("images/\(x).jpg")
storageRef.downloadURL { (url, error) in
if let error = error{
print(error.localizedDescription)
}
else{
imagesArray.append(url!)
}
if let x = images_list.last{
cell.itemImage.sd_setImage(with: URL(string: x), placeholderImage: UIImage(named: "default"))
}
}
}
完整代码
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TrainingProgramCollectionViewCell", for: indexPath) as! TrainingProgramCollectionViewCell
//cell.featuredImageView.setImage(from: indexPath.item)
cell.titleLabel.text = trainingPrograms[indexPath.item].title
cell.descriptionLabel.text = trainingPrograms[indexPath.item].description
var images_list = [String]()
var imagesArray = [URL]()
for x in images_list{
print(x)
let storageRef = storage.child("images/\(x).jpg")
storageRef.downloadURL { (url, error) in
if let error = error{
print(error.localizedDescription)
}
else{
imagesArray.append(url!)
}
if let x = images_list.last{
cell.itemImage.sd_setImage(with: URL(string: x), placeholderImage: UIImage(named: "default"))
}
}
}
}
尝试此操作下载单个图像,只需从firebase中获取imageUrl
let x: String = "yourImageUrl"
let storageRef = storage.child("images/\(x).jpg")
storageRef.downloadURL { (url, error) in
if let error = error{
print(error.localizedDescription)
}
else{
imagesArray.append(url!)
}
if let x = images_list.last{
cell.itemImage.sd_setImage(with: URL(string: x), placeholderImage: UIImage(named: "default"))
}
}