3
votes

I'm having a few issues with displaying a UIImage in an app that I'm developing. The steps I took to do this are as follows:

1 - Add a UIImageView to my main view controller in the storyboard. I did this using the interface builder.

2 - I then created an IBOutlet for the UIImageView in the viewcontroller.h. I ensured proper referencing of the image view from the outlet.

3 - I set the image for the UIImageView like so:

_fireIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"flame_icon.png"]];

This was done within the viewDidLoad method in the viewcontroller.m file.

Troubleshooting:

  • I checked whether the _fireIcon variable is being set. It is.
  • I checked whether the viewDidLoad method is being called. It is.
  • In addition, the image is displayed when I statically set it via the storyboard itself. But when I do it via code, it does not display.

Let me know if you need any more additional information, or code snippets.

4
have you tried this..... _fireIcon.image = [[UIImage imageNamed:@"flame_icon.png"]; because there is no need to initialized your imageView. - Mayank Jain

4 Answers

10
votes

you need not to initialize an imageview as you are using Interface builder for image view

_fireIcon.image = [UIImage imageNamed:@"flame_icon.png"]; // or  [_fireIcon setImage:[UIImage imageNamed:@"flame_icon.png"]];

This sets the image. When we drop the UI elements using Interface builder , we need not initialize them, as they are taken care by apple.

1
votes

Doing this you reset your property with newly created UIImageView:

_fireIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"flame_icon.png"]];

And your reference to UIImageView from storyboard gets compeletely lost.
Instead you want to set image:

_fireIcon.image = [UIImage imageNamed:@"flame_icon.png"];
0
votes

Firstly, you do need to allocate the Element created via Interface builder, you just need to set the image to UIImageView directly like this :-

[myImage setImage:[UIImage imageNamed:@"5.jpg"]];

Make sure image name is correct and its extension too. I just tried and its working great here. Let me know if you still face problem here.

0
votes

Swift 3.0

statusImageView?.image = UIImage(named: "select.png")