【发布时间】:2017-08-21 14:22:45
【问题描述】:
我正在创建一个锻炼应用程序,并创建了一个 UITableview 来显示所有可用的练习(大约 120 个)。我正在使用 NSFetchedResultsController 从 CoreData 获取数据。我遇到的问题是,当我滚动表格时,我的 CPU 使用率非常高。它的范围从 50%-100% 取决于我滚动的速度。
这是我的行单元格:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? ExerciseCell else {fatalError("Couldn't load appropriate index path for custom cell")}
let exercise = self.fetchedResultsController.object(at: indexPath)
cell.exerciseTitle.text = exercise.name
if let getImage = exercise.image2 {
let image = UIImage(data: getImage as Data)
cell.exerciseImage.image = image
} else {
cell.exerciseImage.image = nil
}
return cell
}
我不确定这是否与我转换图像的方式有关?或者我如何设置我的 CoreData?任何帮助都是极好的。谢谢。
【问题讨论】:
-
我怀疑重复调用
UIImage(data:非常昂贵。如果这是我的代码,我可能会尝试获取保存在 CoreData 中的所有练习的图像并将它们缓存在某个地方以便快速重用/重新加载。也许在显示 tableview 之前?
标签: swift xcode uitableview core-data