使用 QuartzCore 在 UITableViewCell 中的 UIImageView 上圆角时,滚动体验缓慢
我使用 QuartzCore
在 UITableView
的单元格内向我的 UIImageView
添加圆角,
这是我使用的代码:
fooImageView.layer.cornerRadius = 9.0;
fooImageView.layer.masksToBounds = YES;
fooImageView.layer.borderWidth = 1.0;
问题是当我添加这段代码时。表细胞的运动显着减慢。我只是想知道是否有其他替代方法可以使用户体验更快并在使用此技术滚动表视图单元格时提高性能?
我看到很多应用程序(大多数 Twitter 应用程序)在单元格内的图像上应用圆角时性能没有下降。只是想知道他们是如何克服“迟缓”的?
感谢您的帮助。
I use QuartzCore
to add rounded corners to my UIImageView
's within the cells of my UITableView
This is the code that I use:
fooImageView.layer.cornerRadius = 9.0;
fooImageView.layer.masksToBounds = YES;
fooImageView.layer.borderWidth = 1.0;
The problem is that when I add this code. The table cells movement is slowed down dramatically. I'm just wondering whether there are other alternatives to make the user experience much faster and to increase performance when scrolling a table view cell using this technique?
I see a lot of applications (most Twitter apps) that have no performance decrease when having rounded corners applied to images within their cells. Just wondering how they overcome the "sluggishness"?
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我要尝试做的第一件事是设置:
这将圆角效果呈现为位图。不久前,我在 UIScrollView 中的视图上使用 CALayer 效果时遇到了一些类似的问题,而此设置极大地提高了性能。
不要忘记进行设置
以防止像素化(以设备错误的分辨率进行光栅化)。
The first thing I would try doing is setting:
This renders the rounded corner effect as a bitmap. I had some similar issues when using CALayer effects on views in a
UIScrollView
a while back, and this setting drastically improved performance.Don't forget to set
to prevent pixelation (rasterization at the wrong resolution for the device).
我使用了 3 种主要技术来提高 UITableView 性能:
始终重用单元格,使用
带标识符的可重用单元出队
创建新细胞时。这
防止操作系统的开销
创造和摧毁很多
快速滚动时的对象。
折叠视图层次结构
细胞。而不是拥有很多
视图和子视图,创建自定义
查看并进行所有单元格绘图
绘制矩形。 Twitter 等应用程序使用
这种方法可以实现超快速的单元格绘制。
确保您的图像不透明。您可以通过确保所有图像资源都没有嵌入 Alpha 通道并将图层的不透明属性设置为 YES 来实现此目的。
示例:
在 cellForRowAtIndexPath 中(表标识符字符串只是创建对相同类型的单元格的引用,并且可以是您喜欢的任何内容):
查看以下链接以获取提高 UITableViews 性能的示例: http://developer.apple .com/library/ios/#samplecode/TableViewSuite/Introduction/Intro.html%23//apple_ref/doc/uid/DTS40007318-Intro-DontLinkElementID_2
希望这有帮助,
戴夫
there are 3 main techniques for improving UITableView performance that I use:
Always reuse cells, use the
dequeuereusablecellwithidentifier
when creating new cells. This
prevents the overhead of the OS
creating and destroying lots of
objects when scrolling fast.
Collapse the view hierarchy of the
cell. Instead of having lots of
views and subviews, create a custom
view and do all your cell drawing in
the drawRect. Apps like Twitter use
this approach for super fast cell drawing.
Ensure your images are opaque. You can do this by ensuring all image assets don't have alpha channels baked into them and by setting the layer's opaque property to YES.
Examples:
In the cellForRowAtIndexPath (the table identifier string simply creates a reference to cells that are the same type and can be anything you like):
Check out the following link for examples of improving performance of UITableViews: http://developer.apple.com/library/ios/#samplecode/TableViewSuite/Introduction/Intro.html%23//apple_ref/doc/uid/DTS40007318-Intro-DontLinkElementID_2
Hope this helps,
Dave
你可以做一些屏蔽。用一个蒙版图像覆盖图像,该蒙版图像有一个圆角正方形,它会像一个魅力一样工作。
还要确保使用reuseCellIdentifier,否则如果表变得有点复杂,表就会滞后。
You could do some masking. Overlaying the images with a mask-image that has a rounded corners square cut out from it and it will work like a charm.
Also be sure to use reuseCellIdentifier otherwise the table will lag if it gets even a little complex.
简短的回答:将单元格设置为不透明并自己绘制它们。
遵循 StuDave 和 Magic Bullet 的建议,另请参阅 Fast Scrolling in Tweetie with UITableView 由官方 Twitter 客户端的作者学习如何进行单元格绘制。这是一个简单明了的示例项目。
除了解决这个具体问题之外,您还应该阅读 UITableView 构造、绘图和管理(重新审视) ),作者:Matt Gallagher,了解如何编写自定义表格控制器和单元格。这不仅可以提高代码的性能,还可以让您完成 Apple 标准类无法完成的事情。基本上,您将创建一个复制
UITableViewController
关键方法的UIViewController
。Short answer: set cells opaque and draw them yourself.
Follow the advice by StuDave and Magic Bullet, and see also Fast Scrolling in Tweetie with UITableView by the author of the official Twitter client ot learn how to do cell drawing. It's a simple and clear example project.
Beyond solving this specific issue, you should read UITableView construction, drawing and management (revisited) by Matt Gallagher to learn how to write custom table controllers and cells. This not only improves the performance of your code, it lets you do things which are not possible with the standard classes from Apple. Basically you will create an
UIViewController
that replicates key methods of theUITableViewController
.就我而言,它添加了无尽的子视图,并且表格视图的速度急剧减慢,正如您所说的那样。我在
willDisplayCell
中添加了这个逻辑来解决这个问题:In my case it added endless subviews and the table view slowed down dramatically exactly as you said. I added this logic in
willDisplayCell
to fix the problem: