xcode 向 UITableView 上的 UIImageView 添加阴影会降低性能......为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9932020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:50:00  来源:igfitidea点击:

Added a shadow to a UIImageView on a UITableView kills performance... why?

iphoneobjective-ciosxcodecocoa-touch

提问by Ethan Allen

I have a UITableViewthat has three UIImageViewviews per cell, with three cell displaying on the view at one time (for a total of nine UIImageViewviews). Think of it as a bookshelf. Sometimes I can have as many as 500 books.

我有一个UITableView具有三个UIImageView每个细胞视图,其中在一个时间视图上三种细胞显示(总共九个UIImageView视图)。把它想象成一个书架。有时我可以拥有多达 500 本书。

I've added shadow to the UIImageViewwith code that is this:

我在UIImageView代码中添加了阴影,代码如下:

UIImageView *itemImageView = [[UIImageView alloc] initWithFrame:CGRectMake(25, 7, 65, 75)];
itemImageView.contentMode = UIViewContentModeScaleAspectFit;
itemImageView.tag = 6;
itemImageView.layer.shadowColor = [UIColor blackColor].CGColor;
itemImageView.layer.shadowOffset = CGSizeMake(3, -1);
itemImageView.layer.shadowOpacity = 0.7;
itemImageView.layer.shadowRadius = 3.0;
itemImageView.clipsToBounds = NO;
[cell.contentView addSubview:itemImageView];

When I add the shadow code, as seen above, scrolling performance is just totally killed and becomes choppy. Each image has a different Rectso the shadow has to be created for each item as it scrolls. Anyone have any tips on how to add shadows to my images on a UITableViewwithout having this issue?

当我添加阴影代码时,如上所示,滚动性能完全被杀死并变得不稳定。每个图像都有不同的,Rect因此在滚动时必须为每个项目创建阴影。任何人都有关于如何在UITableView没有此问题的情况下为我的图像添加阴影的任何提示?

回答by Mark Granoff

You may see a performance improvement if you add

如果添加,您可能会看到性能改进

itemImageView.layer.shadowPath =
       [UIBezierPath bezierPathWithRect:itemImageView.layer.bounds].CGPath;

But in general, layer operations like this will kill performance in a table view. I experienced exactly the same issue, and we just eliminated the shadow effect. It wasn't worth it.

但总的来说,像这样的层操作会降低表视图中的性能。我遇到了完全相同的问题,我们只是消除了阴影效果。这不值得。

回答by Sam

You should review the WWDC videos on CoreAnimation best practices. You can request that the rasterized copy of the shadow be cached in memory. Cocoa is definitely fast enough to render these shadows on the fly without falling back to a pre-rendered image.

您应该查看有关 CoreAnimation 最佳实践的 WWDC 视频。您可以请求将阴影的光栅化副本缓存在内存中。Cocoa 绝对足够快,可以即时渲染这些阴影,而不会退回到预先渲染的图像。

Example:

例子:

itemImageView.layer.shouldRasterize = YES;

Also I up-voted the answer regarding UIBezierPath. This is also mentioned in the best practices, but setting the shadow path of a CALayer is a huge performance boost. You can also create some interesting special effects by manipulating the shadow path.

我也对关于 UIBezierPath 的答案投了赞成票。最佳实践中也提到了这一点,但是设置 CALayer 的阴影路径是一个巨大的性能提升。您还可以通过操纵阴影路径来创建一些有趣的特殊效果。

回答by Alex Cio

I have the same problem with adding shadow to labels inside a tableviewcell. I tried to layout the elements inside cellForRowAtIndexPath when the cell will be created:

我在为 tableviewcell 内的标签添加阴影时遇到了同样的问题。当创建单元格时,我尝试在 cellForRowAtIndexPath 中布局元素:

if(cell == nil){
    //layout cell

These optimized the scrolling a little bit, but its quite choppy.

这些优化了滚动一点点,但它非常不稳定。

For optimizing your pictures quality you should add also the rasterizationScale if you activated "shouldRasterize":

为了优化您的图片质量,如果您激活了“shouldRasterize”,您还应该添加 rasterizationScale:

aLabel.layer.shouldRasterize = YES;
aLabel.layer.rasterizationScale = [[UIScreen mainScreen] scale];

Maybe somebody has some ideas how to optimize the code to get the normal iOS scrolling. thx

也许有人对如何优化代码以获得正常的 iOS 滚动有一些想法。谢谢

回答by lorean

Shadows are expensive and will kill your performance.

阴影很昂贵,会影响你的表现。

A better approach is to render the shadowed image in the background, cache/save it and display it on the view when its ready.

更好的方法是在背景中渲染阴影图像,缓存/保存它并在准备好时将其显示在视图上。

Edit: You way wish to look at Core Graphics / CGImage routines. Specifically CGContextSetShadowWithColor will draw you a shadow.

编辑:您希望查看 Core Graphics / CGImage 例程。特别是 CGContextSetShadowWithColor 会为你画一个阴影。

http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Reference/CGContext/Reference/reference.html

http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Reference/CGContext/Reference/reference.html

回答by Matthias

Do you activate reusing? If not, the cells will redone each time the view changes (e.g., during scrolling). That would eat a lot of performance for sure.

你激活重用吗?如果不是,则每次视图更改时(例如,在滚动期间)单元格将重做。那肯定会消耗很多性能。

回答by Hyman Humphries

Check out my same question here: App running slowly because of UIImageViews

在这里查看我的相同问题:App running slow because UIImageViews

I would use this code:

我会使用这个代码:

imageView.layer.masksToBounds = NO;
UIBezierPath *path = [UIBezierPath imageView.bounds];
imageView.layer.shadowPath = path.CGPath;