gpt4 book ai didi

swift - 无法传递从 firebase 数据库下载的图像

转载 作者:搜寻专家 更新时间:2023-10-31 22:53:11 24 4
gpt4 key购买 nike

我无法在从 firebase 数据库检索到的 detailViewController 上显示图像。

我试图通过查看其他示例(例如下面的示例)来解决它,但我无法使其正常工作。 (iOS + Firebase) Unable to pass the Image to the next ViewController from a UITableViewCell

这是我的图片服务:

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

这就是我构建 collectionViewCell 的方式

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])


if let productImageURL = cell?.productImageURL {
cell?.productImageURL = productImageURL
}

return productCell
}

这是我选择一个单元格时发生的情况:

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 中。

最佳答案

我会改变您解决这个问题的方式。

我会说它不起作用的原因是您下载图像的方式。下载图像并设置它们时,我发现最简单的方法是使用 SDWebImage图书馆,为什么要重新发明轮子?

这具有专门的功能,可以完成您当前尝试手动执行的操作。

我要关注的功能是:

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 还为您缓存图像数据。这意味着如果您加载并返回,您的图像将被缓存一段时间。

关于swift - 无法传递从 firebase 数据库下载的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56148346/

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