试图将从 Firebase 加载的 UICollectionView 中的图像传递给另一个 VC

Posted

技术标签:

【中文标题】试图将从 Firebase 加载的 UICollectionView 中的图像传递给另一个 VC【英文标题】:Trying to pass Image from UICollectionView loaded from Firebase, to another VC 【发布时间】:2018-05-31 22:48:29 【问题描述】:

我遇到的问题是将 MainDetailVC 中的图像设置为在主视图控制器中选择的单元格中的图像。我不断得到 desVC.detailImage.image 的 nil 值。如果有人有任何提示,那就太棒了。

对 Swift 来说还是很陌生,所以请原谅我的任何无知。哈哈

谢谢!

import UIKit
import Firebase
import FirebaseDatabase
import SDWebImage

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, ImageCellDelegate 




@IBOutlet weak var popImageCollection: UICollectionView!
@IBOutlet weak var imageCollection: UICollectionView!


var customImageFlowLayout = CustomImageFlowLayout()
var popImageFlowLayout = PopImagesFlowLayout()
var images = [BlogInsta]()
var popImageArray = [UIImage]()
var homePageTextArray = [NewsTextModel]()

var dbRef: DatabaseReference!
var dbPopularRef: DatabaseReference!

override func viewDidLoad() 
    super.viewDidLoad()
    dbRef = Database.database().reference().child("images")
    dbPopularRef = Database.database().reference().child("popular")
    loadDB()
    loadImages()
    loadText()
    customImageFlowLayout = CustomImageFlowLayout()
    popImageFlowLayout = PopImagesFlowLayout()

    imageCollection.backgroundColor = .clear
    popImageCollection.backgroundColor = .white


    // Do any additional setup after loading the view, typically from a nib.

func loadText() 
    dbRef.queryOrdered(byChild: "order").observe(DataEventType.value, with:  (snapshot) in
        if snapshot.childrenCount > 0 
            self.homePageTextArray.removeAll()
            for homeText in snapshot.children.allObjects as! [DataSnapshot] 
                let homeTextObject = homeText.value as? [String: AnyObject]
                let titleHome = homeTextObject?["title"]
                let categoryButtonText = homeTextObject?["category"]
                self.imageCollection.reloadData()
                let homeLabels = NewsTextModel(title: titleHome as! String?, buttonText: categoryButtonText as! String?)
                self.homePageTextArray.append(homeLabels)
            
        
    )

func loadImages() 
    popImageArray.append(UIImage(named: "2")!)
    popImageArray.append(UIImage(named: "3")!)
    popImageArray.append(UIImage(named: "4")!)

    self.popImageCollection.reloadData()

func loadDB() 
    dbRef.queryOrdered(byChild: "order").observe(DataEventType.value, with:  (snapshot) in
        var newImages = [BlogInsta]()
        for BlogInstaSnapshot in snapshot.children 
            let blogInstaObject = BlogInsta(snapshot: BlogInstaSnapshot as! DataSnapshot)
            newImages.append(blogInstaObject)

self.images = newImages
self.imageCollection.reloadData()
)



override func didReceiveMemoryWarning() 
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.


func numberOfSections(in collectionView: UICollectionView) -> Int 
    return 1

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 
    if collectionView == self.imageCollection 
       return images.count
     else 
        return popImageArray.count
    



func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
    if collectionView == self.imageCollection 
   let cell = imageCollection.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! ImageCollectionViewCell
        cell.delegate = self
        cell.contentView.backgroundColor = UIColor.clear
    let image = images[indexPath.row]
        let text: NewsTextModel
        text = homePageTextArray[indexPath.row]
        cell.categoryButton.setTitle(text.buttonText, for: .normal)
        cell.newTitleLabel.text = text.title
    cell.imageView.sd_setImage(with: URL(string: image.url), placeholderImage: UIImage(named: "1"))
        return cell
     else 
       let cellB = popImageCollection.dequeueReusableCell(withReuseIdentifier: "popCell", for: indexPath) as! PopularCollectionViewCell
        let popPhotos = popImageArray[indexPath.row]
        cellB.popularImageView.image = popPhotos
        cellB.popularImageView.frame.size.width = view.frame.size.width
            return cellB
    




func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) 
    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let desVC = mainStoryboard.instantiateViewController(withIdentifier: "MainDetailViewController") as! MainDetailViewController
    let imageIndex = images[indexPath.row]

    let imageURLString = imageIndex.url
    print(imageURLString)
    let imageUrl:URL = URL(string: imageURLString)!
    let imageData: NSData = NSData(contentsOf: imageUrl)!
    let image = UIImage(data: imageData as Data)
    desVC.detailImage.image = image
    performSegue(withIdentifier: "toDetails", sender: self)




func MusicWasPressed() 
    performSegue(withIdentifier: "homeToMusic", sender: self)


func SneakersWasPressed() 
    performSegue(withIdentifier: "homeToSneakers", sender: self)


func RandomWasPressed() 
    performSegue(withIdentifier: "homeToRandom", sender: self)


func FashionWasPressed() 
    performSegue(withIdentifier: "homeToFashion", sender: self)

func EntertainmentWasPressed() 
    performSegue(withIdentifier: "homeToEntertainment", sender: self)




【问题讨论】:

【参考方案1】:

问题是你这里没有通过

 performSegue(withIdentifier: "toDetails", sender: self)

你创建一个 desVC 没有 push ,并使用 segue ,也不要这样做

let imageData: NSData = NSData(contentsOf: imageUrl)!

因为它阻塞了主线程,我建议发送图像的 url 并将其加载到那里,比如SDWebImage

//

首先要澄清你有两个选择,首先是像你一样创建一个desVC并使用发送的参数初始化它,然后推送到self.navigationController 使用segue当你使用segues并想要发送数据时到目的地实现 prepare(for segue ,所以重构为

1-

 performSegue(withIdentifier: "toDetails", sender: imageURLString )

2-

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) 
    if segue.identifier == "toDetails"  
       let nextScene = segue.destination as? MainDetailViewController 
          nextScene.imageURLString = sennder as! String
          nextScene.sendedTitle = self.sendedTitle
        

    

3- 在显示 destinationVC 之前不要访问 UI 元素

【讨论】:

谢谢!那是我的问题,我将 URL 字符串发送到 detailVC,然后使用 SDWebImage 从那里设置图像 太好了,我喜欢这种快速执行 还有一个问题,如果我想传递另一个字符串,例如单元格的标题怎么办?谢谢。 您可以在当前 VC 中声明另一个 var 说 sendedTitle 然后根据需要在 didSelect 中分配它并通过它查看编辑,最好将整个对象发送到 sender并从中获取两个值,但是我查看了您的模型,如果您可以将它们组合在一个数组中说将它们放入结构中,那么您似乎有标题数组和图像数组,然后您就可以将其发送到 sender 参数中,您也可以在发件人中发送字典,但这不是一个好的 OOP 方法 非常感谢!抱歉,Swift 新手(一般编码)。但我真的很喜欢它!

以上是关于试图将从 Firebase 加载的 UICollectionView 中的图像传递给另一个 VC的主要内容,如果未能解决你的问题,请参考以下文章

react-native 渲染速度比从 firebase 加载数据要快得多

firebase模块的动态加载? (v9)

将从 Fabric 创建的自定义事件迁移到 Firebase?

如何将从 Google Place API 获取的信息存储到 Firebase?

无法将从 Firebase 获取的数据添加到新数组

如何将从firebase存储上传的图像保存到firestore数据库中的currentUser