我想在 collectionView 中使用 JSON 解析加载图像。我正在获取图像 URL 数组,但图像未显示在 UIImageView
上。在获取 JSON 数组并将其显示在 UICollectionView
的 UIImageView
中后,我使用了这段代码。
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
关于ios - 从 JSON 图像 url swift 填充 collectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36308081/