2016-07-21 101 views
0

使用AlamofireImage以異步方式加載圖像。 除非我滾動應用程序崩潰速度非常快,否則它工作得很好。使用AlamofireImage以快​​速滾動的快速UItableview與AlamofireImage崩潰

我認爲這是因爲當超過10個請求在很短的時間內被髮送時,應用程序崩潰(當我快速滾動時)。 我也看到內存使用突然激增。

當我滾動緩慢,也許4個請求在短時間內發送它不會崩潰。

有沒有人有關於如何防止這一點暗示?我如何取消用戶滾動的不可見單元格的請求?

下面是代碼:

// Dequeue your cell and other code goes here. 
// with as! the cell is set to the custom cell class: DemoCell 
// afterwards all data can be loaded from the JSON response into the cells 
override func tableView(tableView: UITableView, cellForRowAtIndexPath 
    indexPath: NSIndexPath) -> UITableViewCell { 

    let cell = 
     tableView.dequeueReusableCellWithIdentifier("FoldingCell", 
                forIndexPath: indexPath) as! DemoCell 
    cell.delegate = self 

    //tag the cell with the indexpath row number to make sure the loaded asynch image corresponds to the right cell 
    cell.tag = indexPath.row 
//clear cell of eventually reused images 
    cell.schoolCoverImage.image = UIImage() 
    cell.schoolBiggerImage.image = UIImage() 

//TODO: set all custom cell properties here (retrieve JSON and set in cell), use indexPath.row as arraypointer 

    let resultList = self.items["result"] as! [[String: AnyObject]] 
    let itemForThisRow = resultList[indexPath.row] 

    cell.schoolNameClosedCell.text = itemForThisRow["name"] as! String 
    cell.schoolNameOpenedCell.text = itemForThisRow["name"] as! String 

    self.schoolIdHelperField = itemForThisRow["name"] as! String 

    cell.schoolIntroText.text = itemForThisRow["name"] as! String 

// set the button's tag like below. 
    cell.innerCellButton.tag = indexPath.row 

//call method when button inside cell is tapped 
    cell.innerCellButton.addTarget(self, action: #selector(MainTableViewController.cellButtonTapped(_:)), forControlEvents: .TouchUpInside) 


     cell.schoolIntroText.text = "We from xx University..." 

//handle the image from a separate API call 
    let schoolIdNumber = itemForThisRow["sco_id"] as! NSInteger 
    let schoolIdString = String(schoolIdNumber) 
//TOCHeck: maybe Id is not correct and should be replaced by indexCount 


    let imageNameString = itemForThisRow["image"] as! String 


//only load the image of the cell which is visible in the screen 
//  print("current cells visible?") 
// print(tableView.visibleCells) 
// print("currentCell") 
// print(cell.tag) 
// if(tableView.visibleCells.contains(cell)) { 

    let urlRequest = NSURLRequest(URL: NSURL(string: "https://ol-web- test.herokuapp.com/olweb/api/v1/schools/"+schoolIdString+"/image/"+imageNameString)!) 

    print(urlRequest) 

    //does cell number/tag match current indexpath row? 
    if(cell.tag == indexPath.row) { 

    //use cache in case image has been saved to cache already, otherwise get image from networking 

    if(self.photoCache.imageForRequest(urlRequest) != nil) { 

     cell.schoolCoverImage.image = photoCache.imageForRequest(urlRequest) 
     cell.schoolBiggerImage.image = photoCache.imageForRequest(urlRequest) 
     print("image from cache loaded") 
    } 
    else 
    { 

     self.imageDownloader.downloadImage(URLRequest: urlRequest) { response in 
      print(response.request) 
      print(response.response) 
      debugPrint(response.result) 

      if let image = response.result.value { 
       print("here comes the printed image:: ") 
       print(image) 
       print(schoolIdString) 

       //set image to the cell 

        cell.schoolCoverImage.image = image 
        cell.schoolBiggerImage.image = image 

        self.photoCache.addImage(image, forRequest: urlRequest) 
        print("image from network loaded and added to cache") 
        print(self.photoCache.memoryCapacity.description) 
        print(self.photoCache.memoryUsage.description) 

      } 
    } 
     } 
} 

    return cell 
} 

編輯:登錄錯誤是空指針

30/image/Beet_Language_Bournemouth_1.jpeg } 
fatal error: unexpectedly found nil while unwrapping an Optional va lue 

代碼行:

let urlRequest = NSURLRequest(URL: NSURL(string: "https://ol-web- test.herokuapp.com/olweb/api/v1/schools/"+schoolIdString+"/image/"+imageNameString)!) 

我在這裏裝載從該PARAMS schoolIdString和imageNameString先前的查詢。

+0

檢查此[MJ-TableView-Image-Swift](https://github.com/JaleelNazir/MJ-TableView-Image-Swift) –

+0

會很好if您可以從崩潰中粘貼錯誤日誌。 –

+0

它應該來自您的圖片下載器。嘗試使用儀器檢查https://www.raywenderlich.com/97886/instruments-tutorial-with-swift-getting-started – xmhafiz

回答

0

Thx爲答案。這是來自數據庫的腐敗數據,使得URL損壞