user3578956
user3578956

Reputation: 31

Need help programmatically setting a UIImageVIew Image

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

Upvotes: 0

Views: 132

Answers (2)

Logan
Logan

Reputation: 53112

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");
    }
}

Upvotes: 1

Sergey Neskoromny
Sergey Neskoromny

Reputation: 1208

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

Upvotes: 2

Related Questions