开发者

Background image in view controller result in memory increase + UIColor colorWithPatternImage

I tried 2 different methods of creating a background image for a view controller.

I have researched this before and came to the conclusion that for good memory practice you should use this method:

NSString *path = [[NSBundle mainBundle] pathForResource:@"controllerBackground" ofType:@"png" inDirectory:@""];

[sel开发者_开发技巧f.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:path]]];

When using the other method:

[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"controllerBackground.png"]]];

This is not as efficient as using the imageWithContentsOfFile:path method.

However when testing both these methods using the performance tools "leak" monitor, I observed a very unusual thing happening.

While using the imageWithContentsOfFile:path method, the problem was that when going back and forth within a navigation controller loading the view controller, the memory would keep increasing within the vicinity of approximately 1mb every time the view controller loads.

This problem is also only observed in the new iOS 4.1 and 4. It does not seem to happen in the iOS 3.0 or 3.2.


Here's a fix that worked for me.

UImage *image = [UIImage imageNamed:@"name.png"];
self.view.layer.contents = (id) image.CGImage;

You have to import QuartzCore, now i don't have 2MB memory increase every time i go that view.


Allan, I faced the same problem and found that colorWithPatternImage and initWithPatternImage is taking large memory than expected. Here is a link which explains the same.

http://www.cocoabyss.com/coding-practice/colorwithpatternimage-abusive-memory-usage/

Better to avoid both methods for background images.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜