我有UITableView和UITableViewCell,它包含6个UIImageView:2(140x140)和4个(18x18)以及其他一些视图。
我还使用了所有最佳实践,例如:
但我的iPad滚动性能有问题。有6个可见细胞(iPad专业版9.7),滚动性能非常糟糕。我发现,当我评论设置图像滚动效果非常好,所以,图像的问题。此外,我使用Time Profiler并检测到我的代码运行速度很快,但在设置图像时系统库会做很多工作。
那么,有没有办法改善设置和滚动性能?
func setImage(from string: String?, to view: UIImageView) {
DispatchQueue(label: "com.myCompany.image-converter", qos: .userInitiated).async {
let imageData = Data(base64Encoded: string ?? "", options: Data.Base64DecodingOptions(rawValue: 0))
if let correctImageData = imageData {
let sourceImage = UIImage(data: correctImageData)
let size = CGSize(width: view.bounds.width, height: view.bounds.height)
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
sourceImage?.draw(in: rect)
let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
DispatchQueue.main.async {
view.image = resizedImage
}
}
}
}
在这里,我调用自定义UITableViewCell中的update方法设置图像。
DispatchQueue.global(qos: .userInitiated).async {
let imageConverter = ImageBase64Converter()
imageConverter.setImage(from: contest.contestPlayerOne.player.image, to: self.leftPlayerImageView)
imageConverter.setImage(from: contest.contestPlayerTwo.player.image, to: self.rightPlayerImageView)
imageConverter.setImage(from: contest.contestPlayerOne.schedule.teamHome.image, to: self.leftTeamLeftPlayerImageView)
imageConverter.setImage(from: contest.contestPlayerOne.schedule.teamAway.image, to: self.rightTeamLeftPlayerImageView)
imageConverter.setImage(from: contest.contestPlayerTwo.schedule.teamHome.image, to: self.leftTeamRightPlayerImageView)
imageConverter.setImage(from: contest.contestPlayerTwo.schedule.teamAway.image, to: self.rightTeamRightPlayerImageView)
}