插入一个NSString到NSBundle的pathForResource中?(Inserting an NSString into NSBundle's pathForResource?)

编程入门 行业动态 更新时间:2024-10-07 12:18:59
插入一个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"];

更多推荐

本文发布于:2023-08-02 09:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1372675.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NSBundle   NSString   Inserting   pathForResource

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!