문제

이것은 Impact DrawArray에서 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;

컴파일과 실행이지만 이미지는 표시되지 않습니다.

도움이 되었습니까?

해결책

사용 +stringWithFormat:, 당신의 예에서와 마찬가지로 :

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

다른 팁

NSString* imageName = [self dynamicallyCalculateImageNameUsingCrazyMath];
UIImage* myImage = [UIImage imageNamed:imageName];
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top