ios - 从 JSON 图像 url swift 填充 collectionView

标签 ios json swift image uicollectionview

我想在 collectionView 中使用 JSON 解析加载图像。我正在获取图像 URL 数组,但图像未显示在 UIImageView 上。在获取 JSON 数组并将其显示在 UICollectionViewUIImageView 中后,我使用了这段代码。

    if let url = NSURL(string: self.Products[indexPath.row].image) {
        if let data = NSData(contentsOfURL: url){
            cell.product_image.contentMode = UIViewContentMode.ScaleAspectFit
            cell.product_image.image = UIImage(data: data)
        }
    }

但是我无法加载图像,但显示了文本。我在 cellForItemAtIndexPath 方法中使用了这段代码。任何人都可以告诉我我做错了什么吗?

func jsonParsingFromURL(){
        // 1
        let reposURL = NSURL(string: "http://api.room2shop.com/api/product/GetProducts?categoryId=24&filter=2&pageNumber=1")
        // 2
        if let JSONData = NSData(contentsOfURL: reposURL!) {
            // 3
            do
            {
                if let json = try NSJSONSerialization.JSONObjectWithData(JSONData, options: .AllowFragments) as? NSDictionary {
                    // 4
                    if let reposArray = json["ProductList"] as? [NSDictionary] {
                        // 5
                        for item in reposArray {
                            Products.append(ProductList(json: item))
                        }
                    }
                }
            }
            catch {
                print("Error with Json: \(error)")

            }
        }
    }

这是我的JSON解析代码

最佳答案

为了反射(reflect)变化,你需要使用

self.collectionView?.reloadData()

编辑

1-请用这个替换这个 block ,并告诉我你是否正常获取 url,对我来说我正常获取 url

                for item in reposArray
                {
                    //ProductList(json: item)
                    //Products.append(ProductList(json: item))
                    //print(item)
                    print("__________")
                    print(item["Image"]!)

                }

2-我得到了

传输安全已阻止明文 HTTP

解决方案在这里。 Transport security has blocked a cleartext HTTP

关于ios - 从 JSON 图像 url swift 填充 collectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36308081/

相关文章:

java - Android java 类有很多监听器接口(interface)

iphone - 查找附近的 iOS 设备

ios - 导航栏中的 SearchController 问题

android - 适用于 iPhone/Android 的图像识别库/API

javascript - csvtojson 转换器忽略我的文件名,只是将 undefined

php - 刷新评论区

ios - 无法写入 Safari 上的输入字段

php - jQuery $.each 循环和 json 数据

Swift 将命令传递到 jar 文件中

ios - UISearchBar 底线