2017-03-06 154 views
0

我想從api中獲取圖像,從而獲得圖像的url,我是否需要另一個API調用以從它們的url下載圖像?如何抓取每個圖像並將其附加到數組中並將其顯示在我的收藏視圖單元格中。我宣佈這個類範圍 var videoArray = [funnyVideos]()從API獲取圖像

Alamofire.request(_url).responseJSON { (response) in 

    print(response.result.value!) 
    if let dict = response.result.value as? Dictionary<String, AnyObject> { 
     if let result = dict["result"] as? Dictionary<String, AnyObject> { 
      if let data = result["data"] as? [Dictionary<String, AnyObject>] { 

       for obj in data { 
        let mainObj = funnyVideos() 
        mainObj.parseData(from: obj) 
        videoArray.append(mainObj) 
        // print (obj) 
       } 

       if let image1 = data[0]["image"] as? String {   
        print(image1) 
       } 

       self.collection.reloadData() 
      } 
     } 
    } 
    completed() 
} 

這個功能之外的另一個類:搞笑視頻

func parseData(from: Dictionary<String, AnyObject>){ 
    if let name = from["title"] as? String{ 
     self._name = name.capitalized 
    } 
} 

本功能配置單元保存用於收集標籤和ImageView的網點另一個類細胞。

func configureCell(_ funny : funnyVideos) { 
    self.funny = funny 
    nameLabel.text = self.funny.name.capitalized 
    // imageView.image = UIImage() 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt  indexPath: IndexPath) -> UICollectionViewCell { 
    fun = videoArray[indexPath.row] 
    cell.configureCell(fun) 
} 

我在這裏有什麼選擇來從UIImage容器中的url中抓取它們?和print(image1)打印圖像的URL。

+0

修復代碼格式 –

回答

0

試試吧,

可以使用AlamofireImage下載圖像,並設置爲的CollectionView。

0

是的,你需要調用圖像,從JSON獲取網址,擺脫URL圖像您可以使用 使用這樣

FUNC的CollectionView(_的CollectionView:UICollectionView,cellForItemAt indexPath:IndexPath) - > UICollectionViewCell {

guard let identifier = cellIdentifier else{ 
     fatalError("Cell identifier not provided") 
    } 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier , 
                    for: indexPath) as UICollectionViewCell 

讓IMAGEURL = arrayImage [indexPath.row]

cell.imageView.setImage(URL: 「IMAGEURL」)

恢復單元

}

https://github.com/rs/SDWebImage https://github.com/onevcat/Kingfisher

0

是的,你必須使圖像的URL另一個API調用。您可以使用AlamofireImage或sdwebimage庫下載圖像。

先用正常的Alamofire庫打開Api並獲取所有圖像的鏈接並將它們保存爲字典或數組。然後使用AlamofireImage或sdwebimage進行另一個API調用。

你應該嘗試sdwebimage becauz很容易

0

你應該使用一個名爲Kingfisher 它會處理緩存以及圖像的下載,當你提供的URL的雨燕庫。 這是入門的鏈接。 Cheat Sheet