3

I have a pretty big view which is like 1000 x 1000. So I did clipToBounds = YES. Will this actually produce smaller bitmaps for the views?

3 Answers 3

3

clipsToBounds drastically decreases performance. Try putting a bunch of views in a scroll view with clipsToBounds turned on, then add some content inside the view at areas it will actually have to clip (for example, the view has a corner radius), run and scroll. The scrolling will be very jerky.

You're not making the processor's job "easier" by telling it to render less, you're giving it MORE work to do which is deciding what to draw and what not to draw. That takes up cycles.

1
  • 1
    I just found this out the hard way -- then did a Google search for "clipsToBounds jerky" and found this thread. I've got a bunch of views in a scroll view, each view with clipsToBounds turned on. These views have rounded corners and my scrolling is jerky. I'm considering creating a subview within each view (with regular square corners) and trying it out with clipsToBounds set on that instead of the ones with rounded corners. Jun 29, 2012 at 19:43
1

I think probably not - you will still need to allocate the memory for the view, just not display it.

You will probably get a very slight performance increase as UIKit knows more about what / what not to draw.

However, you can answer this question yourself! Make a project that makes 1000 1000x1000 views and puts them on the screen. Then look at it's memory usage in the profiler. Then do the same with clipToBounds set to YES and see it it's changed!

I predict that it won't but I'd be interested in the results - if you do it, please post them up here!

Thanks, Sam


EDIT

After the very reasonable comment by mystify, I've tested it by doing the following:

1) Created a new XCode project - Window application

2) Used the following code :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch
    [window setClipsToBounds:YES];
    [window makeKeyAndVisible];

    // Make some views
    for (int n = 0; n < 100000; ++n) {
        UIImageView *view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.jpg"]];
        [view setFrame:CGRectMake(0, 0, 1000, 1000)];
        [window addSubview:view];
        [view release];
    }

    return YES;
}

3) Built it

4) Ran it in Instruments - Object allocations from a fresh start of the simulator.

As predicted, no change - it takes about 16MB with clipsToBounds set to either YES or NO on the window.

However, there are still a few questions -

a) I used a UIImageView to make sure that I had content that needed to be drawn for the entire of my view - the UI layer might optimize differently for different subclasses of UIView.

b) I ran it in the simulator (I don't have an iPhone handy, mine broke) - I'm 99% sure that it won'e be any different on a device but I also know that the simulator isn't 100% accurate with memory leaks so it's probably a good idea to test on a device as well.

1
  • why don't you do it? It's a 150 rep bounty ;-) (but thanks for the hint, gonna try it soon) Apr 17, 2010 at 13:25
1

I don't think so. clipsToBounds refers to subviews, not to the view itself. The UIView documentation suggests using CATiledLayer for especially large views. CATiledLayer should lower your memory footprint since it only loads the sections of the view that are actually displayed to the user (kind of like Google Maps loads map tiles).

EDIT: By the way, UIKit is smart enough not to render stuff that's not visible, either because it's out of the screen or obscured by other views. However, since you're already holding the entire view in memory this won't probably help with memory usage but only with rendering speed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.