【问题标题】:How to show image from json in table view如何在表格视图中显示来自json的图像
【发布时间】:2018-07-03 11:35:20
【问题描述】:

为了获取数据,创建了 from json 类

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"]!
    }
}

在表格视图中显示数据

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
    }

json 遵循格式

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;
})

这个 json 有以下图片来自

images = "7a53f87882409c4622a0977d5026038b.jpg";

如何以字符串格式显示图像 .Image(classObject.image) 如何在表格视图中显示图像视图?您可以从此链接下载代码。https://drive.google.com/file/d/1bVQsuSQINSa6YRwZe2QwEjPpU_m7S3b8/view?usp=sharing

【问题讨论】:

  • 图片的基本 URL 在哪里?只有我们有文件名。我们需要图像在它所在的服务器中的路径。
  • 我只在 json 中提供了文件名 .Base url 或文件路径没有在 json 中提供

标签: ios json api web-services uitableview


【解决方案1】:

图片路径应该是URL 格式。 http://dev.nsol.sg/projects/sneakers/assets/brand_images/thumb/7a53f87882409c4622a0977d5026038b.jpg.

你的JSON

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

全球宣言:

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()
    }

【讨论】:

  • 让 imageURLPathString = newsurl + images ....image=7a53f87882409c4622a0977d5026038b.jpg...newsurl=的值是什么?..
  • 在你的 JSON 响应中。
  • 我应该为 newsurl= 提供什么值?
  • 代码更新。检查一次。
  • 我已更新完整代码。 JSON 部分填充也完成了。
猜你喜欢
  • 1970-01-01
  • 2020-06-04
  • 2014-05-05
  • 1970-01-01
  • 1970-01-01
  • 2013-06-01
  • 1970-01-01
  • 2011-10-22
  • 1970-01-01
相关资源
最近更新 更多