如何将用户图像从 Tablecell 传递到 firebase 上的 Viewcontroller?

How to pass user image from Tablecell to Viewcontroller on firebase?

我只能将用户名标签从 table 单元格传递到另一个 Viewcontroller 而不是用户图像。我尝试使用 prepare for segue 函数,您可以在下面的代码中看到。谢谢

import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase
import FirebaseStorage

class MainVc: UIViewController, UITableViewDelegate, UITableViewDataSource {




@IBOutlet weak var tableview2: UITableView!
var user = [User]()

override func viewDidLoad() {
    super.viewDidLoad()



    retrieveUsers()


}

func retrieveUsers() {
    let ref = Database.database().reference()

    ref.child("users").queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in

        let users = snapshot.value as! [String : AnyObject]
        self.user.removeAll()
        for (_, value) in users {
            if let uid = value["uid"] as? String {
                if uid != Auth.auth().currentUser!.uid {
                    let userToShow = User()
                    if let fullName = value["full name"] as? String,let businessType = value["Business Type"] as? String, let imagePath = value["urlToImage"] as? String {

                        userToShow.fullName = fullName
                        userToShow.businessType = businessType

                        userToShow.imagePath = imagePath
                        userToShow.userID = uid

                        self.user.append(userToShow)


                    }

                }



            }



        }


        self.tableview2.reloadData()
    })

    ref.removeAllObservers()

}




 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {



    let upcoming: ViewController = segue.destination as! ViewController

    let indexPath = self.tableview2.indexPathForSelectedRow!

    let titleString = self.user[indexPath.row].fullName


    let imageView = self.user[indexPath.row].imagePath! // i tried using this to transfer the image  from tableview to viewcontroller but its not working 

    upcoming.imageView = imageView

    upcoming.titleString = titleString


    self.tableview2.deselectRow(at: indexPath, animated: true)



}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return user.count ?? 0
}



func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableview2.dequeueReusableCell(withIdentifier: "userCell", for: indexPath) as! UserCell


    cell.userNameLbl.text = self.user[indexPath.row].fullName
     cell.businessTypeLbl.text = self.user[indexPath.row].businessType

    cell.userID = self.user[indexPath.row].userID
    cell.userImage.downloadImage(from: self.user[indexPath.row].imagePath!)

    return cell
}    
}   

extension UIImageView {

    func downloadImage(from imgURL: String!) {
        let url = URLRequest(url: URL(string: imgURL)!)

        let task = URLSession.shared.dataTask(with: url) {
            (data, response, error) in

            if error != nil {
                print(error!)
                return
            }

            DispatchQueue.main.async {
                self.image = UIImage(data: data!)
            }

        }

        task.resume()
    }


    }

class ViewController: UIViewController {

@IBOutlet weak var profileImage: UIImageView!

@IBOutlet weak var testLbl: UILabel!

var titleString: String!
var imageView: String!



override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

     self.testLbl.text = self.titleString
     self.profileImage.image = UIImage(named: imageView!)



}

导入 UIKit

class 用户:NSObject {

var userID: String!
var fullName: String!
var imagePath: String!
var businessType: String!

}

您可以在 ViewController 中定义 UIImage 或图像名称 属性 并从 MainVc 访问它。我在下面传递了图像名称。

class ViewController: UIViewController {

     @IBOutlet weak var profileImage: UIImageView!

     @IBOutlet weak var testLbl: UILabel!

     var titleString: String!
     var yourImageName:String!
     override func viewDidLoad() {
     super.viewDidLoad()

     self.testLbl.text = self.titleString
     self.imageview.image = UIImage(named:yourImageName)
}

并在准备转场时将图像名称传递为

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    let upcoming: ViewController = segue.destination as! ViewController
    let indexPath = self.tableview.indexPathForSelectedRow!
    let titleString = self.user[indexPath.row].fullName
    let imageName = self.user[indexPath.row].imagename // access image name from user array
    upcoming.titleString = titleString
    upcoming.yourImageName = imageName

    // here i want to pass user image as welH from cell to the viewcontroller
    self.tableview.deselectRow(at: indexPath, animated: true)
 }

希望对您有所帮助。快乐编码!!

self.profileImage.self.downloadImage(来自:imageString)