есть два контроллера SearchViewController, DetailInfoViewController. В модели Movie через Alamofire делаю запросы к API в ответ получаю инфо о фильме. Также там есть линк на постер к фильму. Как его вытащить в UIImage в 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
}
}