iosjsonswiftuitableviewweb-services

How to show image from JSON in table view


To get data the from JSON class is created:

class getData: NSObject {

    var descriptionn : String = ""
    var image : String = ""

//    static let shared = getData()

    func getDataForTableView(results: [[String:String]], index : Int){

        var productArray = [String:String]()
        productArray = results[index]

        descriptionn = productArray["description"]!
        image = productArray["images"]!
    }
}

To display data in table view:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "discoveryNewscell") as! DiscoveryNewsTableViewCell

//        if results.count > 0{
            classObject.getDataForTableView(results: results, index: indexPath.row)
            cell.sneakerImageView.image=filteredsneakernews[indexPath.row].image
                   print("abc image"+classObject.image)
        cell.newsTitle.text = classObject.descriptionn
//        }
        return cell
    }

The json is following foramt

ptional({
    data =     (
                {
            "created_date" = "2017-11-09 14:58:58";
            "created_on" = "2017-11-09";
            "createdd_by" = 3;
            description = dfdfdsdfsdfs;
            id = 4;
            images = "7a53f87882409c4622a0977d5026038b.jpg";
            likes = 25;
            product = 12;
            status = 1;
            title = facebook;
            "title_keys" = fac;
            type = News;
        },
                {
            "created_date" = "2017-11-15 14:33:01";
            "created_on" = "2017-11-23";
            "createdd_by" = 3;
            description = dfdfdf;
            id = 7;
            images = "e626b8003e6e08df874e33556e7f6e69.jpg";
            likes = 3;
            product = 0;
            status = 1;
            title = don;
            "title_keys" = don;
            type = News;
        },
                {
            "created_date" = "2017-11-16 10:34:48";
            "created_on" = "2017-11-13";
            "createdd_by" = 3;
            description = "my first computer";
            id = 8;
            images = "556b1855de039b8d99bc787acd103262.jpg";
            likes = 90;
            product = 13;
            status = 1;
            title = Dell;
            "title_keys" = dell;
            type = News;
        },
                {
            "created_date" = "2018-01-02 16:23:54";
            "created_on" = "2018-01-08";
            "createdd_by" = 3;
            description = sdfsdfsfdf;
            id = 14;
            images = "0c980d3f9cd0393a46bb04995d16177a.jpg";
            likes = 0;
            product = 0;
            status = 1;
            title = dfsfdsfdfs;
            "title_keys" = " dfsfdsfdfs";
            type = News;
        }
    );
    message = "Success.";
    newsurl = "http://dev.nsol.sg/projects/sneakers/assets/brand_images/thumb/";
    success = 1;
})

This JSON has image of following from

images = "7a53f87882409c4622a0977d5026038b.jpg";

How to display the image .Image(classObject.image) in string format how to display image view on table view ?you can download the code from this link .https://drive.google.com/file/d/1bVQsuSQINSa6YRwZe2QwEjPpU_m7S3b8/view?usp=sharing


Solution

  • Image path should be like URL format. http://dev.nsol.sg/projects/sneakers/assets/brand_images/thumb/7a53f87882409c4622a0977d5026038b.jpg.

    yourJSON

    message = "Success.";
    newsurl = "http://dev.nsol.sg/projects/sneakers/assets/brand_images/thumb/";
    success = 1;
    

    Global Declaration:

    var imgURLStringArr = [String]()
    
    
    func appendURLPathFromJSON(jsonDict: NSDictionary)
    {
            let urlPath : String = jsonDict.value(forKey: "newsurl") as! String
            let dataArr : NSArray = jsonDict.value(forKey: "data") as! NSArray
    
            for i in 0..<dataArr.count
            {
                let dataDict : NSDictionary = dataArr[i] as! NSDictionary
                let imageStr : String = dataDict.value(forKey: "images") as! String
                let appendPath : String = urlPath + imageStr
                imgURLStringArr.append(appendPath)
            }
    }
    
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    
            print("willDisplaywillDisplay")
            let cell = cell as! DiscoveryNewsTableViewCell
    
            URLSession.shared.dataTask(with: NSURL(string: self.imgURLStringArr[indexPath.row])! as URL, completionHandler: { (data, response, error) -> Void in
    
                if error != nil {
                    print(error ?? "No Error")
                    return
                }
                DispatchQueue.main.async(execute: { () -> Void in
                    let image = UIImage(data: data!)
                    cell.thumbImg.image = image
                })
    
            }).resume()
        }