0

My idea is to add generated integer value to the name of UIImageView. Here is an example: I have three UIImageViews named imageView1, imageView2, imageView3. When a button is pressed I have there this simple code: self.value = value + 1; (and of course I have this "value" declared) Now I want add some code which says something like: UIImage *img = [UIImage imageNamed:@"image.png"]; [imageView(value) setImage:img]; So this I want this part of code ...[imageView(value)... to use the value I defined above. How could I do this?

TomasJ
  • 289
  • 8
  • 21

3 Answers3

1

You can set a tag for your image views:

// set tag
[imageView1 setTag:1];
[imageView2 setTag:2];
//...

// get the image view with the tag
[(UIImageView *)[self.view viewWithTag:value] setImage:img];
//...
Kjuly
  • 34,476
  • 22
  • 104
  • 118
  • I tried it but nothing happen...no error but also no image shows. here is my code: `UIImage *img = [UIImage imageNamed:@"image.png"]; [(UIImageView *)[self.view viewWithTag:value] setImage:img];` – TomasJ Sep 29 '12 at 09:24
  • And in view did load I put `[imageView1 setTag:1]; [imageView2 setTag:2];` – TomasJ Sep 29 '12 at 09:27
  • @TomasJ what's the value of `cislotatku`? It must be 1~3 if you set tags: 1, 2, 3 (only numeric is allowed). – Kjuly Sep 29 '12 at 09:27
  • It's allways previous value + 1. In viewdidload it's declared as 0 so when button is first pressed it's 0+1 then again 1+1 etc – TomasJ Sep 29 '12 at 09:32
  • @TomasJ please put a NSLog for your value & image in button pressed method, check whether it is right or not. – Kjuly Sep 29 '12 at 09:39
  • I did. It is right (both value and image) I can't find out where is the problem. – TomasJ Sep 29 '12 at 09:41
  • @TomasJ that's weird.. Have you added your image views to your self.view as subviews?? I think it's an another issue in your code then. – Kjuly Sep 29 '12 at 09:42
  • I'm not sure I understand what you mean. How do you mean as subviews? – TomasJ Sep 29 '12 at 09:45
  • I'm sorry my bad It's OK now I put these imageviews in wrong place. – TomasJ Sep 29 '12 at 09:49
  • @TomasJ __subviews__: [self.view addSubview:imageView1]; :) – Kjuly Sep 29 '12 at 09:57
1

You could use a tag, or you can put the UIImageViews into an NSArray. Then you can do something like

[((UIImageView *)[imageViewArray objectAtIndex:self.value]) setImage:img];
Dima
  • 23,484
  • 6
  • 56
  • 83
1

Here I tried to did according to your requirement.

my ViewController Class

- (void)viewDidLoad
{   
[super viewDidLoad];
ExtendedUIImageView *eimage=[[ExtendedUIImageView alloc]initExtendedUIImageViewWithFrame:CGRectMake(0, 0, 768, 1024)];
UIImage *img1=[UIImage imageNamed:@"mac.png"];
UIImage *img2=[UIImage imageNamed:@"images.jpeg"];
UIImage *img3=[UIImage imageNamed:@"mac.png"];
[eimage addUIImage:img1 ToExtendedImageViewWithRect:CGRectMake(100, 100, 100, 100) withTag:1];
[eimage addUIImage:img2 ToExtendedImageViewWithRect:CGRectMake(210, 100, 100, 100) withTag:2];
[eimage addUIImage:img3 ToExtendedImageViewWithRect:CGRectMake(320, 100, 100, 100) withTag:3];
[self.view addSubview:eimage];


}

- (IBAction)chageImage:(UIButton *)sender {
    NSLog(@"#####");
     UIImage *img2=[UIImage imageNamed:@"images.jpeg"];
    [eimage replaceImage:img2 forTag:3];
}

I Extended the UIView Class and created new Class ExtendedImageView class

ExtendedImageView.h

@interface ExtendedUIImageView : UIView
-(id)initExtendedUIImageViewWithFrame:(CGRect)rect;
-(void)addUIImage:(UIImage *)img ToExtendedImageViewWithRect:(CGRect)rect withTag:(int)n;
-(void)replaceImage:(UIImage *)newImg forTag:(int)n;    
@end

ExtendedImageView.m

@implementation ExtendedUIImageView
-(id)initExtendedUIImageViewWithFrame:(CGRect)rect{
    if([super initWithFrame:rect]){
        return  self;
    }
    return nil;
}

-(void)addUIImage:(UIImage *)img ToExtendedImageViewWithRect:(CGRect)rect withTag:(int)n{
    UIImageView *imgView=[[UIImageView alloc]initWithImage:img];
    //[imgView setBackgroundColor:[UIColor grayColor]];
    imgView.frame=rect;
    [imgView setTag:n];
    [self addSubview:imgView];

}

-(void)replaceImage:(UIImage *)newImg forTag:(int)n{
    for(UIView * img in [self subviews]){
        if([img isKindOfClass:[UIImageView class]]&&[img tag]==n){
            ((UIImageView *)img).image=newImg;
        }
    }
}
@end
subhash kumar singh
  • 2,716
  • 8
  • 31
  • 43