插入一个NSString到NSBundle的pathForResource中?(Inserting an NSString into NSBundle's pathForResource?)
这为impactdrawarray中的UIImageView设置了动画:
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;既编译和运行,但图像不显示。
This animates the UIImageView in the impactdrawarray:
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"]]; }Nothing appears with this:
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"]];My reference for using NSString (example)
NSString *myString23 = [NSString stringWithFormat:@"$%d", money]; moneylabel.text = myString23;How can I use a variable in the file name? Is there a way to load an image in a resource folder by index? something like imageByIndex:currentanimationframe? I didn't see anything in the UIImage documentation.
Response to comments:
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"]];and
NSString *imagename = [NSString stringWithFormat:@"A%d", ((impact *) [impactarray objectAtIndex:iv]).animframe]; UIImage *myImage = [UIImage imageNamed:imagename]; ((UIImageView *) [impactdrawarray objectAtIndex:iv]).image = myImage;Both compile and run, but the image does not show.
最满意答案
使用+stringWithFormat:就像在你的例子中一样:
[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"explosion%d", myString2] ofType:@"png"];Use +stringWithFormat:, just as in your example:
[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"explosion%d", myString2] ofType:@"png"];更多推荐
发布评论