137

I have an IBOutlet to a UIImageView, but when I look at the UIImageView doc, I can't see any hints about programmatically changing it. Do I have to fetch an UIImage object from that UIImageView?

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
Thanks
  • 40,109
  • 71
  • 208
  • 322

15 Answers15

185

If you have an IBOutlet to a UIImageView already, then all you have to do is grab an image and call setImage on the receiver (UIImageView). Two examples of grabbing an image are below. One from the Web, and one you add to your Resources folder in Xcode.

UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://farm4.static.flickr.com/3092/2915896504_a88b69c9de.jpg"]]];

or

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

Once you have an Image you can then set UIImageView:

[imageView setImage:image];

The line above assumes imageView is your IBOutlet.

That's it! If you want to get fancy you can add the image to an UIView and then add transitions.

P.S. Memory management not included.

Jordan
  • 21,746
  • 10
  • 51
  • 63
  • 1
    thanks, but it doesn't work :( i removed the image in interface builder so the UIImageView is empty. then i implemented this into the viewDidLoad method. But nothing happens. Image name is 100% correct specified. I also assigned it with setImage to the outlet. Anything else I can try? – Thanks Apr 01 '09 at 16:44
  • 3
    You probably haven't connected the IBOutlet to the ViewController reference in IB. Check the image is in the Resources folder. – Jordan Apr 01 '09 at 22:05
  • I'm not sure the necro rules/impact on SO, but I do know this helped me solve my problem. I'm using iOS v5.1, xcode 4.3.2 – Jake Apr 28 '12 at 22:50
  • 1
    Check for [UIViewController viewDidLoad] is called before changing image. I've trapped by it. 8) – poGUIst Nov 02 '12 at 13:59
  • 1
    Adding on to this solution, if you are using Image Asset Catalog, just specify the name of your image set e.g UIImage *image = [UIImage imageNamed: @"My Icon"]; instead of UIImage *image = [UIImage imageNamed: @"myicon.png"]; – Keith OYS May 14 '14 at 06:15
37

Note that the NIB file doesn't wire up all the IBOutlets until the view has been added to the scene. If you're wiring things up manually (which you might be doing if things are in separate NIBs) this is important to keep in mind.

So if my test view controller has an "imageView" wired by a nib, this probably won't work:

  testCardViewController.imageView.image = [UIImage imageNamed:@"EmptyCard.png"];
  [self.view addSubview:testCardViewController.view];

But this will:

  [self.view addSubview:testCardViewController.view];
  testCardViewController.imageView.image = [UIImage imageNamed:@"EmptyCard.png"];
samkass
  • 5,774
  • 2
  • 21
  • 25
30

This worked for me

[ImageViewName setImage:[UIImage imageNamed: @"ImageName.png"]];

Make sure that the ImageView is declared properly in the .h file and is linked with the IB element.

Lukas Wiklund
  • 826
  • 6
  • 21
Gilall
  • 301
  • 3
  • 2
24
imageView.image = [UIImage imageNamed:@"myImage.png"];
Lukas Wiklund
  • 826
  • 6
  • 21
17

Example in Swift:

import UIKit

class ViewController: UIViewController {

    @IBOutlet var myUIImageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func myAction(sender: UIButton) {
        let newImg: UIImage? = UIImage(named: "profile-picture-name")
        self.myUIImageView.image = newImg
    }

    @IBAction func myAction2(sender: UIButton) {
        self.myUIImageView.image = nil
        self.myUIImageView.image = UIImage(data: NSData(contentsOfURL: NSURL(string: "http://url/image.png")!)!)
    }

}
King-Wizard
  • 15,628
  • 6
  • 82
  • 76
16

For the purpose of people who may be googling this to try to solve their problem, remember to properly declare the property in your header file and to synthesize the UIImageView in your implementation file... It'll be tough to set the image programmatically without getter and setter methods.

#import <UIKit/UIKit.h>

@interface YOURCONTROLLERNAME : UIViewController {
    IBOutlet UIImageView *imageToDisplay;
}

@property (nonatomic, retain) IBOutlet UIImageView *imageToDisplay;

@end

and then in your .m :

@implementation YOURCONTROLLERNAME

@synthesize imageToDisplay;
//etc, rest of code goes here

From there you should be fine using something like the following to set your image.

[YOURCONTROLLER.imageToDisplay setImage:[UIImage imageNamed:value]];
swiecki
  • 3,483
  • 3
  • 23
  • 19
  • Thanks! Being new to xcode and iphones in general I was missing the YOURCONTROLLER before the imageToDisplay. – Opy Jun 10 '11 at 18:36
10

Don't forget to call sizeToFit() after you change image if you then use size of UIImageView to set UIScrollView contentSize and/or compute zoom scale

let image = UIImage(named: "testImage")
imageView.image = image
imageView.sizeToFit()
scrollView.contentSize = imageView.bounds.size
Miroslav Hrivik
  • 822
  • 13
  • 16
9

Following Jordan's advice (which should work actually), try to set the UIImageView to be visible:

 [imageView setHidden: NO];

and also - don't forget to attach it to the main UIView:

[mainView addSubview: imageView];

and to bring to the front:

[mainView bringSubviewToFront: imageView];

Hope combining all these steps will help you solve the mystery.

Agent Chocks.
  • 1,312
  • 8
  • 19
Adi
  • 2,033
  • 5
  • 23
  • 26
8

My problem was that I tried to change the image in an other thread. I did like this:

- (void)changeImage {
    backgroundImage.image = [UIImage imageNamed:@"img.png"];
}

Call with:

[self performSelectorOnMainThread : @selector(changeImage) withObject:nil waitUntilDone:NO];
Okku
  • 1,167
  • 1
  • 13
  • 23
  • 2
    Yup, this bit me... again... Can also use `dispatch_async(dispatch_get_main_queue(), ^{backgroundImage.image = [UIImage imageNamed:@"img.png"];})` – Dex Sep 17 '12 at 03:11
7

If you want to set image to UIImageView programmatically then Dont Forget to add UIImageView as SubView to the main View.

And also dont forgot to set ImageView Frame.

here is the code

UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];

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

[self.view addSubview:myImage];
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
4
myUIImageview.image = UIImage (named:"myImage.png")
Helton Wernik
  • 185
  • 2
  • 3
4

Working with Swift 5 (XCode 10.3) it's just

yourImageView.image = UIImage(named: "nameOfTheImage")
bytepunch
  • 71
  • 3
2
UIColor * background = [[UIColor alloc] initWithPatternImage:
    [UIImage imageNamed:@"anImage.png"]];

self.view.backgroundColor = background;
[background release];
Perception
  • 79,279
  • 19
  • 185
  • 195
2

This question already had a lot of answers. Unfortunately none worked for me. So for the sake of completenes I add what helped me:

I had multiple images with the same name - so I ordered them in sub folders. And I had the full path to the image file I wanted to show. With a full path imageNamed: (as used in all solutions above) did not work and was the wrong method.

Instead I now use imageWithContentsOfFile: like so:

self.myUIImage.image = [UIImage imageWithContentsOfFile:_currentWord.imageFileName];

Don't know, if anyone reads that far?

If so and this one helped you: please vote up. ;-)

DerWOK
  • 1,061
  • 9
  • 13
0

To set image on your imageView use below line of code,

self.imgObj.image=[UIImage imageNamed:@"yourImage.png"];                         
S. S
  • 169
  • 3
  • 16
Vaibhav Shiledar
  • 939
  • 8
  • 15