Swift 无法传递从firebase数据库下载的图像
我无法在从firebase数据库检索的detailViewController上显示该图像 我试图通过查看其他示例(如下面的示例)来解决这个问题,但我无法让它正常工作。 这是我的图像服务:Swift 无法传递从firebase数据库下载的图像,swift,firebase,swift5,Swift,Firebase,Swift5,我无法在从firebase数据库检索的detailViewController上显示该图像 我试图通过查看其他示例(如下面的示例)来解决这个问题,但我无法让它正常工作。 这是我的图像服务: import Foundation import UIKit import IHProgressHUD class ImageService { static let cache = NSCache<NSString, UIImage>() static func dow
import Foundation
import UIKit
import IHProgressHUD
class ImageService {
static let cache = NSCache<NSString, UIImage>()
static func downloadImage(withURL url:URL, completion: @escaping (_ image:UIImage?, _ url:URL)->()) {
let dataTask = URLSession.shared.dataTask(with: url) { data, responseURL, error in
var downloadedImage:UIImage?
if let data = data {
downloadedImage = UIImage(data: data)
}
if downloadedImage != nil {
self.cache.setObject(downloadedImage!, forKey: url.absoluteString as NSString)
}
DispatchQueue.main.async {
completion(downloadedImage, url)
IHProgressHUD.dismiss()
}
}
dataTask.resume()
}
static func getImage(withURL url:URL, completion: @escaping (_ image:UIImage?, _ url:URL)->()) {
if let image = cache.object(forKey: url.absoluteString as NSString) {
completion(image, url)
} else {
downloadImage(withURL: url, completion: completion)
}
}
}
这是我选择单元格时发生的情况:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView == self.vegatableCollectionView {
performSegue(withIdentifier: "showVegetableDetail", sender: veggies[indexPath.item])
}
}
我就是这样说的:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showVegetableDetail" {
let destination = segue.destination as! VegetableDetailViewController
destination.selectedProduct = sender as! Product
}
}
最后这是我的detailViewController:
var veggies = [Product]()
var veggieRef: DatabaseReference?
var selectedProduct: Product?
weak var cell:Product?
@IBOutlet weak var headerTitle: UILabel!
@IBOutlet weak var itemDescription: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
scrollView.delegate = self
getTheData()
}
func getTheData() {
headerTitle.text = selectedProduct?.productName
itemDescription.text = selectedProduct?.productDescription
}
}
我只想从数据库中获取图像并在detailViewController中显示它。我会改变您处理此问题的方式 我想说它不起作用的原因是你下载图像的方式造成的。当下载图像并设置它们时,我发现最简单的方法是使用库,为什么要重新发明轮子 它具有专门的功能,可以完成您当前尝试手动执行的操作 我将重点介绍的功能是:
imageView.sd_setImage(with: URL(string: "http://www.imageURL.com/path/to/image.jpg"), placeholderImage: UIImage(named: "placeholder.png"))
此函数将从URL设置图像。它包括下载图像,然后进行设置,同时还允许您在加载图像时通过要显示的占位符图像
在这种情况下,您的代码将简化为:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView === self.vegatableCollectionView {
let productCell = collectionView.dequeueReusableCell(withReuseIdentifier: "vegatable", for: indexPath) as! MainCollectionViewCell
productCell.set(cell: veggies[indexPath.row])
productCell.imageView.sd_setImage(with: cell?.productImageURL, placeholderImage: UIImage(named: "loading_vege.png"))
return productCell
}
}
只需从firebase下载包括图像url在内的信息,即可简化一切。然后可以将其传递到单元格并加载
奖金:
SDWebImage还为您缓存图像数据。这意味着,如果您加载并返回,您的图像将被缓存一段时间 这真的救了我的命@simon_smiley。在过去的10天里,我一直在努力解决这个问题。实际上,我遇到了SDWebImage,但不知道如何实现它。但我错过的部分实际上就在那里。您的建议比使用我下载和获取数据的方式简单得多。谢谢你抽出时间。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView === self.vegatableCollectionView {
let productCell = collectionView.dequeueReusableCell(withReuseIdentifier: "vegatable", for: indexPath) as! MainCollectionViewCell
productCell.set(cell: veggies[indexPath.row])
productCell.imageView.sd_setImage(with: cell?.productImageURL, placeholderImage: UIImage(named: "loading_vege.png"))
return productCell
}
}