2 exit door eXit_door 于 2013.06.04 11:30 提问

获取当前位于uiimageView中的图片名称

UIImageView中用计时器实现5张图片不断变换。能不能获取当前位于uiimage的图片名称?查询了一些方法,设置标签等等,写了如下代码:

[_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]]];
}

2个回答

Mr_me
Mr_me   2013.06.04 14:46
已采纳

使用图片属性的可访问标示符Accessibility identifier,在你的方法中设置图片 accessibility identifier,你就可以随时随地获取图片了

-(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]);
}
zhanglei5415
zhanglei5415   Rxr 2013.06.04 13:21

给imgView设置tag

[_imgView setImage:[UIImage imageNamed:@"tops.png"]];
[_imgView setTag:0]; //tops.png在imgArray中的索引位置
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; 
    int curTag=_imgView.tag;
    NSLog(@"current imageView name is :%@",[imgArray objectAtIndex:curTag]);
    [_imgView setImage:[UIImage imageNamed:[imgArray objectAtIndex:randNum]]];
    [_imgView setTag:randNum];
}
Csdn user default icon
上传中...
上传图片
插入图片