1

I am using a UIImageView and about 5 images that is changing time to time with the help of timer. Can we get currently set image name in UIImage.I google it and found a that by inserting image names to array and by setting tag to imageView we can get image name by its tag but in my case there is only one imageView and images are 5.My code is given below:--

[_imgView setImage:[UIImage imageNamed:@"tops.png"]];
imgArray=[[NSArray alloc] initWithObjects:@"tops.png",@"position_conv.png",@"stocks.png",@"home.png",@"report.png",nil];
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(change) userInfo:nil repeats:YES];

-(void)change
{
    int randNum = rand() % (4 - 0) + 0; 
    [_imgView setImage:[UIImage imageNamed:[imgArray objectAtIndex:randNum]]];
}

Thanks!

2 Answers2

2

you can use Accessibility identifier property of UIImageView's image.Set image accessibility identifier in your method and get anywhere you want.

-(void)change
{
    int randNum = rand() % (4 - 0) + 0;
    [_imgView setImage:[UIImage imageNamed:[imgArray objectAtIndex:randNum]]];
    [_imgView.image setAccessibilityIdentifier:[imgArray objectAtIndex:randNum]];
}


- (IBAction)currentImgName:(id)sender {
    NSLog(@"image name is %@", [_imgView.image accessibilityIdentifier]);
}
Prince Kumar Sharma
  • 12,591
  • 4
  • 59
  • 90
0

You can use setAccessibilityIdentifier method for any subclass of UIView

UIImageView *image ;
[image setAccessibilityIdentifier:@"file name"] ;

NSString *file_name = [image accessibilityIdentifier] ;
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
  • read question carefully.I said I have only one UIImageView. So your answer every time gives me same value when I get imageName. –  Jun 03 '13 at 10:06
  • it is clear from the answer enough it can hold a string value and yeah it is enough.whenever you change image you set that value and you have what you want. – Lithu T.V Jun 03 '13 at 10:25
  • sorry, it was not clear from your answer because you didn't specify the location where to set accessibility of imageView.I think it will be in viewDidload so I put this code and checked. –  Jun 03 '13 at 12:30
  • any way +1 for you... Accept it. –  Jun 03 '13 at 12:31