(iOS + Firebase) Невозможно передать изображение следующему ViewController из UITableViewCell - PullRequest
0 голосов
/ 05 декабря 2018

У меня есть UITableView, где данные поступают из базы данных Firebase RealtimeDatabase.Как только пользователь выберет строку, данные из строки, а именно: Заголовок, Описание и Изображение, будут переданы следующему ViewController.

Я могу передать заголовок и описание, но не могу передать изображение.

Вот мой код для UITableView:

import UIKit
import Firebase

class PostTable: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var tableView:UITableView!

    var posts = [Post]()

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView = UITableView(frame: view.bounds, style: .plain)
        view.addSubview(tableView)

        let cellNib = UINib(nibName: "PostTableViewCell", bundle: nil)
        tableView.register(cellNib, forCellReuseIdentifier: "postCell")
        var layoutGuide:UILayoutGuide!

        layoutGuide = view.safeAreaLayoutGuide

        tableView.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor).isActive = true
        tableView.topAnchor.constraint(equalTo: layoutGuide.topAnchor).isActive = true
        tableView.trailingAnchor.constraint(equalTo: layoutGuide.trailingAnchor).isActive = true
        tableView.bottomAnchor.constraint(equalTo: layoutGuide.bottomAnchor).isActive = true

        tableView.delegate = self
        tableView.dataSource = self
        tableView.tableFooterView = UIView()
        tableView.reloadData()


        observePosts()
    }

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

    func observePosts() {
        let postsRef = Database.database().reference().child("Data")
        print(postsRef)
        postsRef.observe(.value, with: { snapshot in
            var tempPosts = [Post]()

            for child in snapshot.children{

                if let childSnapshot = child as? DataSnapshot,
                    let dict = childSnapshot.value as? [String:Any],
                    let title = dict["title"] as? String,
                    let logoImage = dict["image"] as? String,
                    let url = URL(string:logoImage),
                    let description = dict["description"] as? String{


                    let userProfile = UserProfile(title: title, photoURL: url)
                    let post = Post(id: childSnapshot.key, title: userProfile, description: description, image: userProfile)
                    print(post)
                    tempPosts.append(post)
                }
            }

            self.posts = tempPosts
            self.tableView.reloadData()
        })
    }

    func getImage(url: String, completion: @escaping (UIImage?) -> ()) {
        URLSession.shared.dataTask(with: URL(string: url)!) { data, response, error in
            if error == nil {
                completion(UIImage(data: data!))
            } else {
                completion(nil)
            }
            }.resume()
    }

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

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print(posts.count)
        return posts.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        let cell = tableView.dequeueReusableCell(withIdentifier: "postCell", for: indexPath) as! PostTableViewCell
        cell.set(post: posts[indexPath.row])
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let postsInfo = posts[indexPath.row]
        print(postsInfo)

        let Storyboard = UIStoryboard(name: "Main", bundle: nil)
        let DvC = Storyboard.instantiateViewController(withIdentifier: "PostTableDetailed") as! PostTableDetailed
        DvC.getName = postsInfo.title.title
        DvC.getDesc = postsInfo.description
//        DvC.getImg = postsInfo.title.photoURL
        self.navigationController?.pushViewController(DvC, animated: true)
    }
}

Вот второй ViewControler, у которого есть подробности поста:

import UIKit

class PostTableDetailed: UIViewController {

    var getName = String()
    var getDesc = String()

    @IBOutlet weak var Name: UILabel!
    @IBOutlet weak var Description: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        Name.text! = getName
        Description.text! = getDesc     
    }

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

}

У меня также есть несколько моделей (Post, UserProfile) и Services (UserService и ImageService), пожалуйста, дайте мне знать, если это необходимо для устранения этой проблемы.

Ответы [ 3 ]

0 голосов
/ 05 декабря 2018

В PostDetail ViewController сделайте вот так

import UIKit

class PostTableDetailed: UIViewController {

    var getName = String()
    var getDesc = String()
    var getImg = String()

    @IBOutlet weak var Name: UILabel!
    @IBOutlet weak var Description: UILabel!
    @IBOutlet weak var ImageContainer: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        Name.text! = getName
        Description.text! = getDesc    
        if let image = getImage(url: getImg) { (image)
            ImageContainer.image = image 
        }

    }

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

    func getImage(url: String, completion: @escaping (UIImage?) -> ()) {
        URLSession.shared.dataTask(with: URL(string: url)!) { data, response, error in
            if error == nil {
                completion(UIImage(data: data!))
            } else {
                completion(nil)
            }
        }.resume()
    }

}
0 голосов
/ 05 декабря 2018

Прежде всего, вы можете использовать этот код для загрузки изображения:

let imageCache = NSCache<AnyObject, AnyObject>()

extension UIImageView {

func downloadImageWithUrlString(urlString: String) -> Void {

    if urlString.count == 0 {
        print("Image Url is not found")
        return
    }

    self.image = nil
    if let cachedImage = imageCache.object(forKey: urlString as AnyObject) as? UIImage {
        self.image = cachedImage
        return
    }

    let request = URLRequest(url: URL(string: urlString)!)
    let dataTask = URLSession.shared.dataTask(with: request) {data, response, error in
        if error != nil { return }
        DispatchQueue.main.async {
            let downloadedImage = UIImage(data: data!)
            if let image = downloadedImage {
                imageCache.setObject(image, forKey: urlString as AnyObject)
                self.image = UIImage(data: data!)
            }
        }
    }
    dataTask.resume()
}
}

Теперь, если вы используете модель, которая содержит Title, Description и ImageUrlString, просто передайте выбранный объект моделик следующему viewController.

В следующем ViewController просто вызовите тот же метод, чтобы загрузить изображение, которое вы используете в первом ViewController.Вам не нужно передавать изображение из VC1 в VC2, потому что, возможно, изображение еще не загружено, и вы выбираете строку для перехода к следующему VC.

Итак, здесь простая вещь, которая передает объект моделии вызывает метод загрузки изображения.

0 голосов
/ 05 декабря 2018

если у вас есть imageUrl, все, что вам нужно, это передать его из PostTable в PostTableDetailed и загрузить изображение.

   // PostTable
       func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            let postsInfo = posts[indexPath.row]
            print(postsInfo)

            let Storyboard = UIStoryboard(name: "Main", bundle: nil)
            let DvC = Storyboard.instantiateViewController(withIdentifier: "PostTableDetailed") as! PostTableDetailed
            DvC.getName = postsInfo.title.title
            DvC.getDesc = postsInfo.description
            DvC.getImg = postsInfo.photoURL
            self.navigationController?.pushViewController(DvC, animated: true)
        }

// PostTableDetailed
class PostTableDetailed: UIViewController {

    var getName = String()
    var getDesc = String()
    var imageUrl = ""

    @IBOutlet weak var Name: UILabel!
    @IBOutlet weak var Description: UILabel!
    @IBOutlet weak var imageView: UIImageView!


    override func viewDidLoad() {
        super.viewDidLoad()

        Name.text! = getName
        Description.text! = getDesc 
        updayeImage()    
    }

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

 private func updateImage() {
        URLSession.shared.dataTask(with: URL(string: self.imageUrl)!) { data, response, error in
            if error == nil, let data = data {
                imageView.image = UIImage(data: data)
            } 
            }.resume()
    }

}

Изображение будет показано после завершения задачи.поэтому я предлагаю вам добавить спиннер в imageView.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...