将 NSString 插入 NSBundle 的 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
+stringWithFormat:
,就像您的示例中一样:Use
+stringWithFormat:
, just as in your example: