2009-06-24 8 views
0

Das belebt die UIImageView in der impactdrawarray:Einfügen eines NSString in NSBundle pathForResource?

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

nichts mit dieser erscheint:

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

Meine Referenz für die Verwendung von NSString (Beispiel)

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

Wie ich eine Variable verwenden können im Dateinamen? Gibt es eine Möglichkeit, ein Bild in einem Ressourcenordner nach Index zu laden? etwas wie imageByIndex:currentanimationframe? Ich habe in der UIImage-Dokumentation nichts gesehen.

Antwort auf Kommentare:

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

und

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

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

Beide kompilieren und ausführen, aber das Bild nicht zeigt.

+0

Was haben Sie versucht? Zu sagen, dass es nicht funktioniert, ist ohne eine weitere Erklärung nicht besonders nützlich. –

+0

Die zwei Codeblöcke unter "Antwort auf Kommentare" habe ich ausprobiert. Kompilieren und ausführen, aber kein Bild erscheint. – user128526

Antwort

2

Verwenden +stringWithFormat:, wie gerade in Ihrem Beispiel:

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

NSString * myString2 = [NSString StringWithFormat: @ "A% d", ((Auswirkung *) [impactarray objectAtIndex: iv]). Animframe]; \t ((UIImageView *) [impactdrawray objectAtIndex: iv]). Image = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: [NSString stringWithFormat: @ "Explosion% d", myString2] ofType: @ "png"]]; Immer noch nichts angezeigt (noch kompiliert OK) – user128526

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