xcode 将 NSString 插入到 NSBundle 的 pathForResource 中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1041472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:38:13  来源:igfitidea点击:

Inserting an NSString into NSBundle's pathForResource?

iphonecocoa-touchxcodensstringuiimage

提问by user128526

This animates the UIImageView in the impactdrawarray:

这会为 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"]];
}

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 的参考(示例)

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.

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

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.

两者都编译并运行,但图像不显示。

回答by Jim Puls

Use +stringWithFormat:, just as in your example:

使用+stringWithFormat:,就像在您的示例中一样:

[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"explosion%d", myString2] ofType:@"png"];

回答by slf

NSString* imageName = [self dynamicallyCalculateImageNameUsingCrazyMath];
UIImage* myImage = [UIImage imageNamed:imageName];