»
S
I
D
E
B
A
R
«
Add Background Image To UIView
June 11th, 2009 by Anish Kumar

If you ever need to add a image as background for an UIView then do it like this (Make sure the image is in your app’s resource bundle):-

- (void)viewDidLoad
{
    self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"MyBackground.png"]];
}
About Me: Anish:
Mac OS X software development is my bread winner with over 6 years of experience. Expertise in Color Management, TWAIN Scanner drivers on Mac OS X, Photoshop Filter and Import Plugin development on Mac OS X, iPhone. As an hobby I love to work on PHP, Flex, AIR, Photoshop. Check the 'About' page for more.

4 Responses  
Agha writes:
July 15th, 2009 at 2:08 am

Thanks for tip.
The able code will fail.
Here is the correct code.
UIView *contentView = [[UIView alloc] initWithFrame: [[UIScreen mainScreen] applicationFrame]];
//contentView.backgroundColor = [UIColor redColor];
//contentView.backgroundColor = [UIColor viewFlipsideBackgroundColor];
//contentView.backgroundColor = [UIColor groupTableViewBackgroundColor];
//contentView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"someimage.png"]];
self.view = contentView;

[contentView release];

Tnanks

    Abhijeet writes:
    August 7th, 2009 at 2:12 pm

    Both the code are same. In first one you are allocating color to background and and assigning it an image.

    In second one again the same thing is happening. The only difference is some extra code. This will increase the allocations of objects when viewed in instruments.
    there is no need to to first have a UIview as a separate object then assign it to the main view.

    Simply extra piece of code.

    Thanks.

memory leak writes:
January 22nd, 2010 at 7:38 pm

You have a memory leak there. The correct code is:

UIColor *bkgColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"MyBackground.png"]];
self.view.backgroundColor = bkgColor;
[bkgColor release];

Me writes:
April 11th, 2010 at 12:51 pm

How about:

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];

Leave a Reply

Spam protection by WP Captcha-Free