0

I want to do is set the image of an empty UIImageView but whatever I try, nothing seems to work. Sorry if this is stupid, I am new to Xcode and Obj-C. The UIImageView takes up whole of the view, and there is a button to load the image (I have also tried putting this in the viewDidLoad bit, no success). Here is the code from my ViewController.m:

- (IBAction)buttonChangeImage:(id)sender {
    UIImage *image = [UIImage imageNamed:@"1.jpg"];
    [_imageView setImage:image];
}

Thanks in advance

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 2
    Put a breakpoint and check that image is not nil and _imageView is not nil. If image is nil verify if 1.jpg is in the target. If _imageView is nil check the outlet connection. – djromero Apr 27 '14 at 18:49
  • Thanks it is working now (after a lot of trying!). imageView was nil and fixing it was as simple as redoing the outlet. Thanks! – user3578956 Apr 27 '14 at 19:11

2 Answers2

2

Probably you did not connect IBOutlet _imageView with imageView in XIB or Storyboard.

Sergey Neskoromny
  • 1,188
  • 8
  • 15
1

Just in case you're having trouble interpreting @djromero's comment, here's how you could see if your problem is with the image:

- (IBAction)buttonChangeImage:(id)sender {
    UIImage * imageToAdd = [UIImage imageNamed:@"1.jpg"];
    if (imageToAdd) {
        _imageView.image = imageToAdd;
        NSLog(@"Image exists, if still not working, make sure _imageView is set properly");
    }
    else {
        NSLog(@"There was a problem loading your image, make sure it is named properly");
    }

    if (_imageView) {
        NSLog(@"ImageView exists!");
    }
    else {
        NSLog(@"ImageView does not exist, make sure it is properly connected");
    }
}
Logan
  • 52,262
  • 20
  • 99
  • 128