NSCache 和自定义 UITableView 绘图
我有一个 UITableView
,它使用 UIView
子类作为其 backgroundView
和 selectedBackgroundView
属性。根据位置(顶部/底部圆角)、选择状态和一些自定义细节附件,视图将被重新绘制。
这可能是一项昂贵的操作,因此我正在考虑使用 NSCache 来缓存一些绘制的视图。我目前正在考虑不同的方法:
- 根据其自定义属性缓存视图(如果同一时间多次使用视图则不起作用 -> 失败)
- 根据其自定义属性缓存视图并使用该视图的副本视图(
UIView
不符合NSCopying
协议 -> 失败) - 缓存视图的
UIImage
表示并将它们分配到UIImageView
- 不要使用根本不使用
NSCache
或者根本不使用自定义绘图? - ???
那么 iOS 性能调优器,您更喜欢哪种方法?
提前谢谢!
I have a UITableView
which uses a UIView
subclass for its backgroundView
and selectedBackgroundView
properties. Depending on the position (top/bottom round corners), the selection state and some custom detail accessories the views get redrawn.
This might be an expensive operation so I'm thinking about using NSCache
for caching some of the drawn views. I'm currently thinking of different approaches:
- Cache the view depending on its custom properties (not working if a view is used more than once the same time -> fail)
- Cache the view depending on its custom properties and use a copy of the view (
UIView
does not conform toNSCopying
protocol -> fail) - Cache a
UIImage
representation of the views and assign them in aUIImageView
- Don't use
NSCache
at all or don't use custom drawing at all? - ???
So iOS-performance-tuners, which approach would you prefer?
Thx in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要过早优化!
剪切视图并不是那么昂贵,我在商店里有一些应用程序,它们以 60FPS 的速度滚动,同时使用带有 drawRect 和 roundRect-Mask 的自定义绘制背景。
创建 selectedBackgroundView 的问题就更少了,因为用户主动执行任务(点击),如果生成视图有 50 毫秒的延迟,没有人会注意到。
此外,NSCache 是线程保存的,因此它有点慢,而且很可能比您想要的开销更多。
关于“视图的副本”,我看不出您有任何理由想要这样做。
Don't prematurely optimize!
Clipping the views isn't that expensive, I have a few apps in the store that scroll at 60FPS while using custom drawn background with drawRect and a roundRect-Mask.
Creating the selectedBackgroundView is even less problem, since the user actively does a task (tap) and if there's like 50ms delay for generating the view, nobody will notice.
Further, NSCache is thread save, so it's a bit slower and most likely more overhead than you want.
Regarding "copy of the view" I don't see any reason why you would want that.