开发者

does clipsToBounds = YES reduce my memory footprint?

开发者 https://www.devze.com 2022-12-25 01:33 出处:网络
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?clipsToBounds drastically decreases performan

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?


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号