将 NSString 插入 NSBundle 的 pathForResource 中?

发布于 2024-07-24 23:29:54 字数 2211 浏览 2 评论 0原文

这对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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

高冷爸爸 2024-07-31 23:29:54

使用 +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"];
入怼 2024-07-31 23:29:54
NSString* imageName = [self dynamicallyCalculateImageNameUsingCrazyMath];
UIImage* myImage = [UIImage imageNamed:imageName];
NSString* imageName = [self dynamicallyCalculateImageNameUsingCrazyMath];
UIImage* myImage = [UIImage imageNamed:imageName];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文