Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBrahma Peddibhotla
198 PointsHow 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.
Brahma Peddibhotla
198 Pointsi 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
Treehouse Guest TeacherYou should be able to assign a new image: self.imageview.image = newImage
.
Brahma Peddibhotla
198 PointsThanks Amit will try it and let you know.
Brahma Peddibhotla
198 Pointshi 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
Treehouse Guest TeacherWhere 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
.
Brahma Peddibhotla
198 PointsSo 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
Treehouse Guest TeacherThe 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
.
Brahma Peddibhotla
198 Pointsya i have imageview on my storyboard. thanks. will try it today and let you know.
Kai Aldag
Courses Plus Student 13,560 PointsKai Aldag
Courses Plus Student 13,560 Pointssorry can you clarify what you mean