繁体   English   中英

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

[英]Swift UItableview with AlamofireImage crash when scrolling fast

我正在使用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。

谢谢答案。 它是来自数据库的损坏数据,导致URL损坏

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM