Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clipToBounds and masksToBounds performance issue

I have UIScrollView and number of objects (UIView compositions) with UIImageViews inside them. Some of UIImageViews has round border (I use myImageView.layer.masksToBounds = YES; for this). Other has rectangle borders and part of image in them (I use Clip subviews property in Interface Builder for this).

The issue is that I found that clip properties strongly affect the performance while scrolling:

For iPod touch (4th generation) results of profiling:

  • with enabled clip properties (both or one of them) I have around 30 fps while scrolling
  • with disabled clip properties I have all 60 fps while scrolling

I really need to clip some images to round bounds and other to rectangle bounds (to show part of image). So, here is my question: what ways there are to improve performance? May be there are low level ways to do it (drawRect: or something), or may be it would be useful to play around alfa masking or I just do something wrong?

like image 590
Lloyd18 Avatar asked Jun 15 '12 10:06

Lloyd18


1 Answers

When you have graphically intensive masks and things, a simple and easy way to improve performance (often times dramatically) is to set shouldRasterize to YES on the layer for that item:

#import <QuartzCore/QuartzCore.h>

// ...

view.layer.shouldRasterize = YES;

This will rastersize the view into a buffer, so it isn't constantly re-rendered. This will take up a extra memory for each view, so you should really try and recycle/reuse views as you scroll, similar to how a table view does.

For correct behaviour on retina display you also need to set an appropriate value for rasterizationScale:

view.layer.rasterizationScale = view.window.screen.scale; // or [UIScreen mainScreen]

I've had great success with this for things like scrolling photo galleries, where each item had rounded corners, shadows, etc.

like image 89
Mike Weller Avatar answered Nov 11 '22 04:11

Mike Weller