gpt4 book ai didi

ios - 从 coredata 加载图像,tableview 滚动不流畅

转载 作者:行者123 更新时间:2023-11-29 05:29:57 25 4
gpt4 key购买 nike

我有六个自定义单元格。 3 个单元格包含 imageView 和一些标签,其他单元格仅包含标签。在 viewDidLoad 中,我从 coredata 加载了所有数据并刷新了 tableview ,问题是当表有单元格(没有 imageview 单元格)时,它会平滑滚动,但是当我们添加单元格(有 imageview 单元格)时,它会向下滚动平滑滚动,但当我向上滚动桌面 View 。我尝试降低图像质量但没有成功。怎么解决这个问题

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{
let insuranceCell = tableView.dequeueReusableCell(withIdentifier: "insuranceCell") as! InsuranceCell
let pollutionCell = tableView.dequeueReusableCell(withIdentifier: "pollutionCell") as! PollutionCell
let servicingCell = tableView.dequeueReusableCell(withIdentifier: "servicingCell") as! ServicingCell
let challanPaidCell = tableView.dequeueReusableCell(withIdentifier: "challanPaidCell") as! ChallanPaidCell
let insuranceClaimCell = tableView.dequeueReusableCell(withIdentifier: "insuranceClaimCell") as! InsuranceClaimCell
let fuelRefillCell = tableView.dequeueReusableCell(withIdentifier: "fuelRefillCell") as! FuelRefillCell

let cellArr = sections[indexPath.section].cell
//Loading cell based on array details
switch cellArr[0] {
case is InsuranceDetails:
insuranceCell.setup(object: cellArr[indexPath.row])
return insuranceCell
case is Pollution:
pollutionCell.setup(object: cellArr[indexPath.row])
return pollutionCell
case is Servicing:
servicingCell.setup(object: cellArr[indexPath.row])
return servicingCell
case is ChallanPaid:
challanPaidCell.setup(object: cellArr[indexPath.row])
return challanPaidCell
case is InsuranceClaims:
insuranceClaimCell.setup(object: cellArr[indexPath.row])
return insuranceClaimCell
case is FuelRefills:
fuelRefillCell.setup(object: cellArr[indexPath.row])
return fuelRefillCell
default:
return insuranceCell
}

}


class InsuranceCell: UITableViewCell {

func setup(object : NSManagedObject){
guard let arr = object as? InsuranceDetails else {return}
lbAmountPaid.text = arr.amountPaid
lbAgency.text = arr.agency
lbVehiclevalidfrom.text = arr.vehicleValidFrom
lbVehiclevalidupto.text = arr.vehicleValidUpto
let imageData = arr.insurancePhoto ?? Data()
let image = UIImage(data: imageData)
let compimapge = image?.resized(withPercentage: 0.1)
insurancePhoto.image = compimapge

}

最佳答案

正如 Fabio 所说,不要总是不必要地使所有单元出队。

但是,图像创建和缩放很可能是卡顿的根源。调用 UIImage.resized(withPercentage: 0.1) 时参数的命名表明您的源图像确实很大(您以原始大小的 1/1000 显示它们!?)。如果参数名称具有误导性并且 0.1 实际上意味着 1/10,我建议重命名该参数(也许是 UIImage.resized(withFraction: 0.1))。

话虽如此,请尝试将图像缩放移出主线程。像这样的东西(未经测试):

class InsuranceCell: UITableViewCell {

func setup(object: NSManagedObject, in table: UITableview, at indexPath: IndexPath) {
guard let arr = object as? InsuranceDetails else {return}

lbAmountPaid.text = arr.amountPaid
lbAgency.text = arr.agency
lbVehiclevalidfrom.text = arr.vehicleValidFrom
lbVehiclevalidupto.text = arr.vehicleValidUpto

// Do time consuming stuff in the background
DispatchQueue.global(qos: .userInitiated).async {
let imageData = arr.insurancePhoto ?? Data()
let image = UIImage(data: imageData)

// This will be where all the time is going. 0.1% suggests your source
// image is massively oversized for this usage.
let compimage = image?.resized(withPercentage: 0.1)

// Always back to the main thread/queue for UI updates
DispatchQueue.main.async {
guard let cell = table.cellForRow(at: indexPath) as? InsuranceCell else {
// The cell we wanted to configure is no longer in view
return
}

// Don't be tempted to write straight to `self.insurancePhoto.image`,
// it may already have been reused for a different row
// if the user is scrolling quickly
cell.insurancePhoto.image = compimage
}

}
}
}

它需要额外的参数来检查缩放完成后更新单元格是否仍然合适。

关于ios - 从 coredata 加载图像,tableview 滚动不流畅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57707325/

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