iPhone Development 101

iPhone 101

Code Tips



A UIImage is an object representing image data. A UIImage is typically displayed inside a UIImageView:

UIImage *bgImage = [UIImage imageNamed:@"background.jpg"];
UIImageView *imgView = (UIImageView *)[self.view viewWithTag:20];

imgView.image = bgImage;  // display the bgImage data inside the imgView

The simplest way to load an image is with the imageNamed method, however this loads the data into memory and (generally) doesn't release it, so this is only ideal for small, frequently-used images.

UIImage *bgImage = [UIImage imageNamed:@"background.jpg"];

Use imageWithContentsOfFile to read image data from a file. This method doesn't cache the image to memory.

UIImage *bgImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"background" ofType:@"jpg"]]

Patterned Backgrounds

You can use an image as a repeating background pattern for a UIView with this code (add to your view controller's viewDidLoad method):

self.view.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"denim.jpg"]];

Additional References