Any alternative to get Image name that is currently set to UIImageView?

六月ゝ 毕业季﹏ 提交于 2019-12-11 04:21:26

问题


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!


回答1:


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]);
}



回答2:


You can use setAccessibilityIdentifier method for any subclass of UIView

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

NSString *file_name = [image accessibilityIdentifier] ;


来源:https://stackoverflow.com/questions/16894135/any-alternative-to-get-image-name-that-is-currently-set-to-uiimageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!