V
V
Volodymyr Lavryk2017-04-20 01:08:00
iOS
Volodymyr Lavryk, 2017-04-20 01:08:00

UIImage from JSON file?

there are two controllers SearchViewController, DetailInfoViewController. In the Movie model, through Alamofire, I make requests to the API in response I get information about the movie. There is also a link to the poster for the movie. How to pull it into UIImage in DetailViewController?

mport Foundation
import UIKit
import Alamofire
import AlamofireImage
protocol MovieDelegate {
    func updateMovieInfo()
}

class Movie {
    let omdbUrl = "http://www.omdbapi.com/?"
    var title: String?
    var filmYear: String?
    var poster: String?
    var moviePoster: UIImage?
    var delegete: MovieDelegate!

    func getMovieInfo(title: String, completion: @escaping ()->()){
        let params = ["t": title]
        Alamofire.request(omdbUrl, method: .get, parameters: params).validate(statusCode: 200..<300).validate(contentType: ["application/json"]).responseJSON { (response) in
            switch response.result {
            case .success(let JSON):
                print(response.response!.statusCode)
                print(response.result.description)
                print("validation successful")
                let response = JSON as! NSDictionary
                print(response)
                let status = response["Response"] as! String
                if status == "True" {
                    self.title = (response["Title"] as! String)
                    self.filmYear = (response["Year"] as! String)
                    self.poster = (response["Poster"] as! String)
                    self.delegete.updateMovieInfo()
                    completion()
                } else {
                    print (response["Error"]!)
                    self.title = (response["Error"] as! String)
                    completion()
                }
            case .failure(let error):
                print (error)

            }
        }

    }
}

import UIKit
    class SearchViewController: UIViewController, MovieDelegate {
        var movie = Movie()
        @IBOutlet weak var activityIndicator: UIActivityIndicatorView!
        @IBOutlet weak var searchTextField: UITextField!
        @IBOutlet weak var searchButton: UIButton!
        @IBAction func searchButtonTapped(_ sender: UIButton) {
            activityIndicator.startAnimating()
            DispatchQueue.main.async {
                self.movie.getMovieInfo(title: self.searchTextField.text!, completion: {
                    self.activityIndicator.stopAnimating()
                    self.performSegue(withIdentifier: "movieInfo", sender: self)
                })

            }
        }
        func updateMovieInfo() {
            print(movie.title!)
            print(movie.filmYear!)
        }
        override func viewDidLoad() {
            super.viewDidLoad()
            self.movie.delegete = self
            // Do any additional setup after loading the view, typically from a nib.
        }

        //MARKL: Delegate
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
              let secondVC = segue.destination as! DetailInfoViewController
                secondVC.movieTitle = movie.title!
              //  print(movie.poster)

        }
    }

class DetailInfoViewController: UIViewController, MovieDelegate {

    @IBOutlet weak var posterImageView: UIImageView!
    @IBOutlet weak var filmNameLabel: UILabel!
    @IBOutlet weak var filmYearLabel: UILabel!
    var movie = Movie()
    var movieTitle = ""
    var year = "No Found!"

    override func viewDidLoad() {
        super.viewDidLoad()

        self.movie.getMovieInfo(title: movieTitle ) {
            self.updateMovieInfo()
        }
        self.movie.delegete = self

     }
    func updateMovieInfo() {
        filmNameLabel.text = movie.title
        filmYearLabel.text = movie.filmYear
    }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Globak, 2017-04-20
@Vo0ne

Here is a good method

guard let url = URL(string: movie.poster) else { return }
DispatchQueue.global().async {
    if let data = try? Data(contentsOf: url) {
        DispatchQueue.main.async {
            posterImageView.image = UIImage(data: data)
        }
    }
}

It loads the image asynchronously and then displays it on the screen.
But I would advise you to look towards the Kingfisher library , it also caches photos and you don’t have to make a request to the network every time to get an image

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question