views:

4271

answers:

3

My problem is that I can't seem to get the image from my bundle to display properly. This method is in the view controller that controls the tableview. headerView is loaded with the tableview in the .nib file and contains a few UILabels (not shown) that load just fine. Any ideas?

- (void)viewDidLoad {
    [super viewDidLoad];

    [[self view] setTableHeaderView:headerView];
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *imagePath = [bundle pathForResource:@"awesome_lolcat" ofType:@"jpeg"];
    UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
    imageView = [[UIImageView alloc] initWithImage:image];
}
+2  A: 

FIrst you need to figure out whether your image is loading properly. The quickest way to get an image is to use the UIImage convenience method +[UIImage imageNamed: rawImageName].

Also, is this a UITableViewController? (it's unclear, but implied).

Where is imageView being used? You create it near the bottom, but don't seem to do anything with it. You probably want to create the image view, assign it an image, and then add it as a subview to the headerView.


   //this assumes that headerView is an already created UIView, perhaps an IBOutlet

   UIImage     *image = [UIImage imageNamed: @"awesome_lolcat.jpeg"];
   UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
   [headerView addSubview: [imageView autorelease]];
   [[self view] setTableHeaderView: headerView];

Ben Gottlieb
A: 

Adding the subview programatically worked, but it isn't correctly linked to the UIImageView in Interface Builder. The view I created is (I think) correctly linked to the UIView outlet in my UITableViewController, but when I init my imageView it creates a new view instead of putting the image in the view I already created.

Thanks for your answer.

kubi
+2  A: 

If you've already created an outlet and connected it to a view in Interface Builder, you should use that view, rather than creating a UIImageView on the fly.


   //this assumes that headerView is an already created UIView, perhaps an IBOutlet
   //also, imageViewOutlet is an IB outlet hooked up to a UIImageView, and added as
   //a subview of headerView.

   //you can also set the image directly from within IB
   imageViewOutlet.image = [UIImage imageNamed: @"awesome_lolcat.jpeg"];
   [[self view] setTableHeaderView: headerView];

Ben Gottlieb
Perfect! Thanks Ben.
kubi
upvoted for the lolcat image name. plus its correct.
James Hall