我无法在从 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/
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e
我正在编写一个小脚本来定位aws存储桶中的特定文件,并创建一个临时验证的url以发送给同事。(理想情况下,这将创建类似于在控制台上右键单击存储桶中的文件并复制链接地址的结果)。我研究过回形针,它似乎不符合这个标准,但我可能只是不知道它的全部功能。我尝试了以下方法:defauthenticated_url(file_name,bucket)AWS::S3::S3Object.url_for(file_name,bucket,:secure=>true,:expires=>20*60)end产生这种类型的结果:...-1.amazonaws.com/file_path/file.zip.A
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
我在pry中定义了一个函数:to_s,但我无法调用它。这个方法去哪里了,怎么调用?pry(main)>defto_spry(main)*'hello'pry(main)*endpry(main)>to_s=>"main"我的ruby版本是2.1.2看了一些答案和搜索后,我认为我得到了正确的答案:这个方法用在什么地方?在irb或pry中定义方法时,会转到Object.instance_methods[1]pry(main)>defto_s[1]pry(main)*'hello'[1]pry(main)*end=>:to_s[2]pry(main)>defhello[2]pry(main)
我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类
当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub