gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:13:19 24 4
gpt4 key购买 nike

我使用 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)!)

我在此处加载了先前查询中的参数 schoolIdString 和 imageNameString。

最佳答案

感谢您的回答。数据库中的数据已损坏,导致 URL 损坏

关于ios - 快速滚动时,带有 AlamofireImage 的 Swift UItableview 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38497728/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com