2009-06-24 4 views
0

Ce anime le UIImageView dans le impactdrawarray:Insertion d'un NSString dans pathForResource de NSBundle?

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

Rien n'apparaît avec ceci:

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"]]; 

Ma référence pour l'utilisation NSString (exemple)

NSString *myString23 = [NSString stringWithFormat:@"$%d", money]; 
moneylabel.text = myString23; 

Comment puis-je utiliser une variable dans le nom de fichier? Existe-t-il un moyen de charger une image dans un dossier de ressources par index? quelque chose comme imageByIndex:currentanimationframe? Je n'ai rien vu dans la documentation d'UIImage.

Réponse aux commentaires:

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"]]; 

et

   NSString *imagename = [NSString stringWithFormat:@"A%d", ((impact *) [impactarray objectAtIndex:iv]).animframe]; 
       UIImage *myImage = [UIImage imageNamed:imagename]; 

       ((UIImageView *) [impactdrawarray objectAtIndex:iv]).image = myImage; 

deux compiler et exécuter, mais l'image ne montre pas.

+0

Qu'avez-vous essayé? Dire "ça ne marche pas" n'est pas particulièrement utile sans quelques explications supplémentaires. –

+0

Les deux blocs de code ci-dessous "Réponse aux commentaires" est ce que j'ai essayé. Les deux compilent et courent mais aucune image n'apparaît. – user128526

Répondre

2

Utilisez +stringWithFormat:, comme dans votre exemple:

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

NSString * myString2 = [NSString stringWithFormat: @ "A% d", ((impact *) [impactarray objetAtIndex: iv]). Animframe]; \t ((UIImageView *) [impactdrawarray objetAtIndex: iv]). Image = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: [NSString stringWithFormat: @ "explosion% d", myString2] deType: @ "png"]]; Toujours rien n'apparaît (compile encore OK) – user128526

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