views:

129

answers:

4
UIImage *image = [[UIImage alloc] initWithContentsOfFile:@"popup.png"];
  UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

  [self.view addSubview:imageView];

  [image release];
  [imageView release];

The code sits within a UIViewcontroller object. It compiles fine but, when run, the subview does not appear to be added.

I'm tired.

A: 

Are you sure the code is run? Put a breakpoint in there.

Are you sure the image is created correctly? Check that popup.png can be found.

Try setting the frame and background color for imageView, see if you can get anything to show up.

I don't see anything wrong with the code you posted.

David Kanarek
A: 

initWithContentsOfFile requires the complete file path, not just the file name.

Replace the line where you initialize UIImage with the following:

NSBundle *bundle = [NSBundle mainBundle];
UIImage *image = [[UIImage alloc] initWithContentsOfFile: [bundle pathForResource:@"popup" ofType:@"png"]];

A simpler way will be to use the imageNamed: method of the UIImage class

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"popup.png"]];

And you dont have to worry about the path-to-file details. Just that the initWithImage: method caches the image. Depending upon your application might be good or bad idea.

Hope this helps!

Mihir Mathuria
A: 

Try to use:

UIImage *image = [UIImage imageNamed:@"popup.png"];

and, off course, get rid of the [image release]; because in this solution the image is autoreleased.

Notice that this solution won't be good if you have more than one image with this name (in different folders/groups)...

Michael Kessler
A: 

your imageView needs a frame. imageView.frame = CGRectMake(0,0,320,480);

nathanjosiah