首页 文章

快速滚动时,带有AlamofireImage的Swift UItableview崩溃

提问于
浏览
0

我使用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
}

编辑:日志错误是NullPointer

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 .

1 回答

  • 0

    谢谢答案 . 来自数据库的损坏数据使URL损坏

相关问题