2016-11-22 66 views
0

我想從字符串中下載多個網址,並在collectionView中顯示圖像。我成功顯示了四個圖像(我的屏幕上有4個可見的單元格),當我滾動時,開始下載另外兩個圖像。Swift - AlamofireImage下載多個網址並顯示在CollectionView中

我希望這6個圖像在同一時間下載(我不必滾動開始其他下載)。在此之後,我想要顯示下載和顯示圖像在我的collectionView中花費的總時間。

我做錯了什麼?

這裏是我的代碼:

import UIKit 
import AlamofireImage 
import Alamofire 

class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource { 

    var players = [ 
     ["image_Name":"https://images.unsplash.com/photo-1420768255295-e871cbf6eb81?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&s=4b94ef340bd7f6cac580fbc76af326af"], 
     ["image_Name":"https://images.unsplash.com/photo-1465411801898-f1a78de00afc?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&s=149d27223217c0fa63c7dd8f1e8d23f6"], 
     ["image_Name":"https://images.unsplash.com/photo-1466853817435-05b43fe45b39?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&s=a3b629b7e0c4f710ce119f219ae5b874"], 
     ["image_Name":"https://images.unsplash.com/photo-1467404899198-ccadbcd96b91?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&s=66eef8db7a0aa4119c6c8d7ba211f79f"], 
     ["image_Name":"https://images.unsplash.com/photo-1470322346096-ecab3914cab7?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&s=83863ba23662871baf6434c6000e00bd"], 
     ["image_Name":"https://images.unsplash.com/photo-1473624566182-509e437512f4?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&s=54c3ec6d1fee824d62e6fa76676ddf17"] 
    ] 
    var methodStart = Date() 

    @IBOutlet weak var mycall: UICollectionView! 
    var selectedIndex=[Int]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 


    func numberOfSections(in collectionView: UICollectionView) -> Int { 
     return 1 
    } 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return self.players.count 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell:playerCell = collectionView.dequeueReusableCell(withReuseIdentifier: "playerCell", for: indexPath) as! playerCell 

     let url = URL(string:self.players[indexPath.row]["image_Name"]!) 
     if indexPath.row == 0 { 
      methodStart = Date() 
     } 
     Alamofire.request(url!).responseImage { response in 
      //   debugPrint(response) 
      //   print(response.request) 
      //   print(response.response) 
      //   debugPrint(response.result) 

      if let image = response.result.value { 
       cell.playerImage.image = image 
      } 
     } 

     if indexPath.row == self.players.count - 1 { 
      let methodFinish = NSDate() 
      let executionTime = methodFinish.timeIntervalSince(methodStart as Date) 
      print("Execution time: \(executionTime)") 
     } 

     return cell 
    } 
} 
+0

下載後,在主隊列中更新圖片,併爲圖片設置setneedslayout屬性 – Vinodh

回答

1

問題在你的代碼 -

你說你想要的6幅圖像在同一時間下載,當用戶滾動第五和第六圖像應立即顯示。但這不會發生,因爲滾動時會調用第5個和第6個單元格的cellForItemAtIndexPath函數。這是默認行爲,您不能在用戶滾動之前調用它。

快速修復解決方案 -

下載所有的外cellForItemAtIndexPath功能通過循環玩家的數組,然後重新裝入最後一個下載完成後的集合視圖圖像,就像在任何viewDidLoadviewDidAppear

常駐和最佳的解決方案 -

實施遲緩裝載並且使用任一HenekeSDWebImage以顯示細胞圖像的圖像的高速緩存。並在這些庫中使用prefetch選項在圖像顯示在單元格中之前獲取圖像。要使用預取選項,您需要遍歷數組並將url傳遞給庫的讀取和存儲功能,並根據該庫的語法在單元中加載圖像,並在下載時管理圖像的顯示,以及您不必擔心這一點。

1

當您處理網絡調用時,您應該更新主隊列中的UI。我想在這裏添加一個更有效的點是Alamofire有一個專門用於圖像下載/調整大小的庫。請看看AlamofireImage Library

Alamofire.request(url!).responseImage { response in 
      if let image = response.result.value { 
       DispatchQueue.main.async { 
        cell.playerImage.image = image 
        cell.playerImage.setNeedsLayout() 
       } 
      } 
     }