2009-06-24 54 views
0

该动画在impactdrawarray中的UIImageView:将NSString插入到NSBundle的pathForResource中?

if (((impact *) [impactarray objectAtIndex:iv]).animframe == 70){ 
    ((UIImageView *) [impactdrawarray objectAtIndex:iv]).image = 
      [UIImage imageWithContentsOfFile: 
         [[NSBundle mainBundle] pathForResource:@"explosionA71" 
                 ofType:@"png"]]; 
} 
if (((impact *) [impactarray objectAtIndex:iv]).animframe == 71){ 
     ((UIImageView *) [impactdrawarray objectAtIndex:iv]).image = 
         [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] 
             pathForResource:@"explosionA72" 
               ofType:@"png"]]; 
} 

没有出现与此:

NSString *myString2 = 
      [NSString stringWithFormat:@"A%d", 
        ((impact *) [impactarray objectAtIndex:iv]).animframe; 

((UIImageView *) [impactdrawarray objectAtIndex:iv]).image = 
        [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] 
            pathForResource:(@"explosion%d", myString2) 
               ofType:@"png"]]; 

我使用的NSString(例如)参考

NSString *myString23 = [NSString stringWithFormat:@"$%d", money]; 
moneylabel.text = myString23; 

我如何使用一个变量在文件名中?有没有办法通过索引加载资源文件夹中的图像?像imageByIndex:currentanimationframe?我在UIImage文档中没有看到任何内容。

回应评论:

NSString *myString2 = [NSString stringWithFormat:@"A%d", ((impact *) [impactarray objectAtIndex:iv]).animframe]; 
    ((UIImageView *) [impactdrawarray objectAtIndex:iv]).image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"explosion%d", myString2] ofType:@"png"]]; 

   NSString *imagename = [NSString stringWithFormat:@"A%d", ((impact *) [impactarray objectAtIndex:iv]).animframe]; 
       UIImage *myImage = [UIImage imageNamed:imagename]; 

       ((UIImageView *) [impactdrawarray objectAtIndex:iv]).image = myImage; 

编译和运行,但图像不显示。

+0

你尝试过什么?没有进一步的解释,说“不起作用”并不是特别有用。 – 2009-06-25 17:03:57

+0

“回复评论”下面的两个代码块就是我试过的。编译并运行,但不出现图像。 – user128526 2009-06-29 18:42:05

回答

2

使用+stringWithFormat:,就像在你的榜样:

[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"explosion%d", myString2] ofType:@"png"]; 
0
NSString* imageName = [self dynamicallyCalculateImageNameUsingCrazyMath]; 
UIImage* myImage = [UIImage imageNamed:imageName];