当按钮被点击以跳转至表格 View 时,它需要大约 5 秒的时间来跳转。在它最终 segues 之后,当 tableview 滚动时,它会断断续续,有时甚至会崩溃。 tableview 从本地 json 文件填充并引用本地镜像。图像被优化为小尺寸。是什么导致了这种情况?我该如何优化/修复我的代码以阻止这种情况发生?
import UIKit
class PDList: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var pdTableView: UITableView!
var pdArt = [Decode]()
override func viewDidLoad() {
super.viewDidLoad()
json()
pdTableView.delegate = self
pdTableView.dataSource = self
}
func json() {
let path = Bundle.main.path(forResource: "palmdesert", ofType: "json")
let url = URL(fileURLWithPath: path!)
do {
let data = try Data(contentsOf: url)
self.pdArt = try JSONDecoder().decode([Decode].self, from: data)
}
catch {}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return pdArt.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "pdCell")
let image = UIImage(named: pdArt[indexPath.row].pic)
cell.textLabel?.text = pdArt[indexPath.row].art
cell.detailTextLabel?.text = pdArt[indexPath.row].artist.capitalized
cell.imageView?.image = image
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "pdDetail", sender: self)
self.pdTableView.deselectRow(at: indexPath, animated: true)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? PDDetail {
destination.pdArt = pdArt[(pdTableView.indexPathForSelectedRow?.row)!]
}
}
@IBAction func done(sender: AnyObject) {
dismiss(animated: true, completion: nil)
}
}
JSON 示例:
{
"art": "Agave",
"desc": "Agave by Michael Watling serves as the City's entry monument and is inspired by the imagery of the agave, a succulent native to the desert. The stone forms are representative of Palm Desert's many well-managed resources for survival and growth.",
"artist": "Michael Watling",
"lat": 33.7215,
"long": -116.362,
"pic": "test.png"
}
如果您需要任何其他信息,请告诉我。感谢您的帮助。
最佳答案
我构建了一个示例 UITableView
项目,其中包含 20 张 4032 × 3024
的大型 jpeg 图像的 Assets 库,通过 UIImage(named:)
到我的单元格。
对于 120 x 70
的 ImageView 来说,这大大超出规范,因此最大要求为 360 x 210
。
时间分析器显示了这一点,用于滚动一批全新的 12 个单元格。
每个单元大约 54 毫秒 (650/12)。然而,即使是大型 JPG 也会利用硬件解码,所以不太不好。
60Hz 刷新率的现代 iWidget 为您提供每帧最多 16 毫秒的单元格刷新时间。所以我口吃。
将图像替换为适当缩放的图像 (360 x 210 ) 即可。
每个单元大约 14 毫秒。表格滚动大部分是平滑的。
所以这里的问题可能是其中之一。
- 您的图片太大。确保您的资源的像素大小是 ImageView 大小的
3x
。 - 您的图片不是 PNG 或 JPEG 格式,无法传递给硬件解码器或使用
pngcrush
进行处理。确保您的图像是 PNG 或 JPEG。例如 GIF 会很糟糕。 - 当 JPG 可能更合适时,您正在使用 PNG。真实世界的图像最好是 JPG。艺术线条/纯色最好是 PNG。
关于ios - swift:从本地 json 文件填充的 uitableview 在滚动时严重断断续续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48312813/