Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

How to change the image run time.

i am able to assign the image using the following code once self.imageview = [[UIImageView alloc]initWithImage:img];

where img is assigned different image depending on the conditions.

but unable to change using the above code. even i tied using the setImage but of no use.

sorry can you clarify what you mean

i am trying to change the image at run time. i am generating the random number depending on the random number i am assigning a image.

by using the below line of code i am able to assign the initial image self.imageview = [[UIImageView alloc]initWithImage:img];

when the next event happens i am unable to assign the next image the old image itself is displaying.

1 Answer

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

You should be able to assign a new image: self.imageview.image = newImage.

Thanks Amit will try it and let you know.

hi amit

i tried the below code as you suggested but its not working

@property (strong,nonatomic)IBOutlet UIImageView *imageview ;

 UIImage *img;
    if (index == 0 )

    {
        //img = [UIImage imageNamed:@"A.png"];
        //[imageview setImage:[UIImage imageNamed:@"A.png"]];
        NSLog(@"0");
        img = [UIImage imageNamed:@"A.png"];
        self.imageview.image = img;
    }
    else
    {
        //img = [UIImage imageNamed:@"B.png"];
      //  [imageview setImage:[UIImage imageNamed:@"B.png"]];
        NSLog(@"1");
        img = [UIImage imageNamed:@"B.png"];
        self.imageview.image = img;
}
[self.view insertSubview:self.imageview atIndex:0];
Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

Where do you have the above? If it's in the viewDidLoad method then it only executes once.

The other issue is that you should not be inserting a subview each time you change the image. When you send the message insertSubview the view adds image view to its view hierarchy. But it seems like you already have the imageView in your view hierarchy since its an IBOutlet which means that there is an image view on the storyboard. By sending the message [self.view insertSubview:self.imageview atIndex:0]; you are instructing the view to add the image view behind every other view since your index is 0.

So this means i have to change the index every time i assign the new image and my index should go on increment till the app runs.

Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

The first question is do you have an image view on your storyboard? If so, then you don't need to add it again to your view hierarchy. It already exists. Just change the image without doing insertSubview.

ya i have imageview on my storyboard. thanks. will try it today and let you know.