2016-03-30 52 views
0

我想的CollectionView使用JSON解析加載圖像。我收到了一系列圖片網址,但圖片未在UIImageView上顯示。在獲取JSON數組並在UICollectionViewUIImageView中顯示它之後,我使用了此代碼。填充的CollectionView從JSON圖像URL快捷

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解析代碼

+1

是你曾經做了'self.collectionView?.reloadData()'??? – DeyaEldeen

+0

不..我不是那樣 –

回答

1

,以反映更改,您需要使用

self.collectionView?.reloadData() 

編輯

1-請更換本該塊,如果告訴我你得到的網址通常,對我來說我得到的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

+0

我應該怎麼做?我已經Products.append(產品列表(JSON:項目))後更新了我的JSON解析.. –

+0

try代碼 – DeyaEldeen

+0

確定..我想,讓你知道 –

1

使用本

// taking the URL , then request image data, then assigne UIImage(data: responseData) 
      let imgURL: NSURL = NSURL(string: "www.example.com/image.jpg")! 
      let request: NSURLRequest = NSURLRequest(URL: imgURL) 
      let session = NSURLSession.sharedSession() 
      let task = session.dataTaskWithRequest(request) { 
       (data,response,error) -> Void in 
       if (error == nil && data != nil) { 
        func display_image() { 
        // imageView.post.image = your UIImage 
        self.imageViewPost.image = UIImage(data: data!) 
        } 

        dispatch_async(dispatch_get_main_queue(), display_image) 
       } 
      } 
      task.resume() 
      // end of loading img