0

Basically, I have a main UIImageView which I then programmatically add another Image View (with image) on top. What I would like to do is dynamically swap out the image on click, but when I attempt to do so the image does not show. Here's what I've implemented so far:

In Storyboard: //mainImgView

enter image description here

In.h:

#import "ViewController.h"
@interface ViewController ()
     @property (nonatomic, strong) UIImageView *overlayImgView;
@end

In.m:

-(IBAction) addOverlayImgViewWImgToMainImgView:(UIButton *)sender{    
    UIImage *tempImg1 = [UIImage imageNamed:@"xcode_icon.png"];

    self.overlayImgView = [[UIImageView alloc]initWithImage:tempImg1];
    [mainImgView addSubview:self.overlayImgView];   
}

-(IBAction) swapImg:(UIButton *)sender{
    UIImage *tempImg2 = [UIImage imageNamed:@"another_icon.png"];
    [self.overlayImgView setImage:tempImg2];
}

If someone could please shed some light on what I’m doing wrong, it would be much appreciated. Thank you.

Among the links I've looked at are:

iOS: How to dynamically add image to a parent view?

UIImage imageNamed returns nil

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImageView_Class/Reference/Reference.html#//apple_ref/occ/instm/UIImageView/initWithImage:

Additional Info:

There's already an image on the main Image View and I'm adding a smaller (50x50) Image View (w/ image) to it. When I try to swap it out, the current image is disappearing then replace with a white box.

The image files are 50x50 and are in my 'Supported Files' folder. I do not have a second '@2X' image.

Community
  • 1
  • 1
0--
  • 221
  • 1
  • 6
  • 16
  • Why not just set the mainImageView image? (Instead of adding an overlay)? Or am I misunderstanding your question... – user1947561 Jul 10 '14 at 15:31
  • Its already set with another image (and I'm overlaying it with another). Thanks. – 0-- Jul 10 '14 at 16:29

1 Answers1

0

Try

- (IBAction)swapImg:(UIButton *)sender{
    UIImage *tempImg2 = [UIImage imageNamed:@"another_icon.png"];
    self.mainImgView.image = tempImg2;
}
Fry
  • 6,235
  • 8
  • 54
  • 93
  • Thank you, but the current image still just disappears and the new image (tempImg2) does not appear. – 0-- Jul 10 '14 at 16:45
  • Actually, tried it with another image, did a clean/build, and added '@2x' and worked. Your method works as well. – 0-- Jul 10 '14 at 16:58