Image APIDemo

//  ViewController.swift
//  ImageAPIDemo

import UIKit

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
    
    @IBOutlet var tblView: UITableView!
    
    var dicFromJson = NSDictionary()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        GetMethod()
    }

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

   func GetMethod(){
    
    let url = URL(string: "https://api.themoviedb.org/3/movie/upcoming?api_key=57b899196913bc0e2c252357ac4d7fe7&language=en-US&page=1")
    
    var request = URLRequest(url: url!)
    
    request.httpMethod = "GET"
    
    let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
        if error != nil{
            print(error?.localizedDescription)
        }
        do{
             let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers)
                //print(json)
            
            self.dicFromJson = json as! NSDictionary
            print(self.dicFromJson)
            
            self.tblView.reloadData()

           print((self.dicFromJson.value(forKey: "results") as! NSArray).count)
           
        }catch let error as NSError{
            print(error)
        }
        
                }
        
        task.resume()
        
    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if dicFromJson.count != 0
        {
            return (self.dicFromJson.value(forKey: "results") as! NSArray).count
        }
        else
        {
            return 0
        }
    }
   
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell : TableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
        
       
        
        let ium = ((self.dicFromJson.value(forKey: "results") as! NSArray).object(at: indexPath.row) as! NSDictionary).value(forKey: "poster_path") as! String
        
                    let im = "https://image.tmdb.org/t/p/w500" + ium
        
                    let url = NSURL(string: im)
                    let dataa = NSData(contentsOf: url as! URL)
                    cell.imgView.image = UIImage(data: dataa as! Data)
        
        return cell
    }
    
    
    
}
    

    

Comments