【发布时间】:2018-10-03 09:24:09
【问题描述】:
在我的 iOS 项目中,我使用带有自定义单元格的常规 UICollectionView。
该单元格接收一些属性,当设置其中一个对象时,我对该单元格执行一些 UI 更新。
虽然当我滚动时感觉滚动有点小故障,但我不知道如何改进我的代码以使其运行更流畅。
我已经运行了 Intruments 并运行了 Time Profiler,当这些滞后发生时,主线程 CPU 使用率达到 100%,如下图所示:
追踪仪器的使用百分比:
进一步追踪png_read_IDAT_data
现在是代码:
在我的UICollectionViewController cellForItem(:_)
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: homeCellId, for: indexPath) as! HomePostCollectionViewCell
cell.post = self.posts[indexPath.item]
cell.delegate = self
cell.layer.cornerRadius = 5
cell.layer.borderWidth = 0.7
cell.layer.borderColor = UIColor.rgb(red: 204, green: 204, blue: 204).cgColor
cell.layer.masksToBounds = true
return cell
}
在我的UICollectionViewCell 中,每当设置post 变量时,我都会执行以下UI 更新:
public var post: Post? {
didSet {
guard let post = self.post else { return }
userProfileImageView.image = post.user.profileImage
nameLabel.text = post.user.name
titleTextView.text = post.title
creationDateLabel.text = post.creationDate.timeAgoDisplay()
thumbnailImageView.image = post.thumbnail
if backgroundImageOffset != nil {
setBackgroundImageOffset(imageOffset: backgroundImageOffset)
} else {
setBackgroundImageOffset(imageOffset: CGPoint.zero)
}
}
}
您能帮我追踪问题以及如何改进它吗?
谢谢。
编辑
我已经注释掉了 cell.post= 行,这样主线程 CPU 使用率下降到大约 10%,因此可以确保问题出在 post { didSet{} } 构造函数中。
你能看到任何有用的改进吗?
编辑 2
这里是timeAgoDisplay()函数的代码
func timeAgoDisplay() -> String {
let secondsAgo = Int(Date().timeIntervalSince(self))
let minute = 60
let hour = 60 * minute
let day = 24 * hour
let week = 7 * day
let month = 4 * week
let quotient: Int
let unit: String
if secondsAgo < minute {
quotient = secondsAgo
unit = "second"
} else if secondsAgo < hour {
quotient = secondsAgo / minute
unit = "min"
} else if secondsAgo < day {
quotient = secondsAgo / hour
unit = "hour"
} else if secondsAgo < week {
quotient = secondsAgo / day
unit = "day"
} else if secondsAgo < month {
quotient = secondsAgo / week
unit = "week"
} else {
quotient = secondsAgo / month
unit = "month"
}
return "\(quotient) \(unit)\(quotient == 1 ? "" : "s") ago"
}
【问题讨论】:
-
我猜这个问题是图像的延迟加载。为了测试这个猜测,你需要用你的资源中的一些相当大的图像替换
post.user.profileImage(任何应该做的)。除了减少图像负载外,几乎所有操作都应该相同。所以请报告这是否消除了故障。 -
在你的单元格
cell.layer.shouldRasterize = truecell.layer.rasterizationScale = UIScreen.main.scale后添加这2行或参考这个链接:stackoverflow.com/questions/18460655/… -
@MaticOblak 让我测试一下
-
@Kuldeep 我会将这些行添加到测试中
-
@ManishM.Mahajan 我实际上在后台线程上进行所有处理,当它们准备好时我分派到主线程。现在我得出结论,问题出在缩略图上,我认为这些可能太大了......我正在再次进行一些测试
标签: ios swift uicollectionview uicollectionviewcell