App running slowly because of UIImageViews

走远了吗. 提交于 2020-01-31 18:28:25

问题


I have three large UIImageViews displaying images within my iPad app (each is almost the size of the screen, and they have special effects such as rotation, shadows, etc to look like a news stack). When these images are displayed, the app runs VERY SLOWLY. UIAlertViews literally look like they have only two frames when they are presented and animations aren't even laggy... they're worse! But when I do not present the UIImageViews, everything works quickly and elegantly. Obviously I'm doing something incorrectly since iOS can surely handle three images. Any suggestions on how to make the app run more quickly? Thanks.

PS I don't even want to know what will happen when I double the resolution of the images for the new iPad haha

Edit: Here is the code I am using to set the shadows. This utilizes the QuartzCore framework.

page2.layer.shadowColor = [UIColor blackColor].CGColor;
page2.layer.shadowOpacity = 1.0;
page2.layer.shadowRadius = 10.0;
page2.layer.shadowOffset = CGSizeMake(0, 4);

Edit 2 (Answer): It appears that the lag occurs because of the way I am setting the shadows. If you set the shadowPath property to be a UIBezierPath of the bounds of the UIImageViews, rendering occurs more quickly and smoothly and the app speeds up significantly. Here is my final code:

page2.layer.shadowColor = [UIColor blackColor].CGColor;
page2.layer.shadowOpacity = 1.0;
page2.layer.shadowRadius = 10.0;
page2.layer.shadowOffset = CGSizeMake(0, 4);
page2.layer.masksToBounds = NO;
UIBezierPath *path2 = [UIBezierPath bezierPathWithRect:page2.bounds];
page2.layer.shadowPath = path2.CGPath;

回答1:


We can only guess without seeing the actual code.

  • Shadows can be extremely expensive. Does it get faster if you turn them off, or decrease the shadowRadius?
  • Setting the shadowPath property can lead to a huge improvement, if you can use it to get the effect you're looking for.
  • Otherwise: bake the shadows into your images, or use some other technique to fake the shadows around the edges of your images.



回答2:


If you use CALayer shadows, that may be a major performance problem for any kind of animations. They trigger the software renderer, which hurts animations really badly. If you have rectangular shadows, it's best to draw the shadow in, say, Photoshop and use it as a stretchable image in separate views/layers. If your views have irregular shapes, you can draw them with Core Graphics, and that will still be much faster when animations take place.




回答3:


The best solution would be to render your shadows as an image. However, I expect performance using CALayer shadowRadius will be greatly improved if you set the shadowPath manually.

Otherwise, it has to be calculated by the system, every frame. Setting it yourself will boost animation performance ridiculously.



来源:https://stackoverflow.com/questions/9756235/app-running-slowly-because-of-uiimageviews

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!