본문 바로가기

프로그래밍/아이폰 프로그래밍

6. NSMutableArray 사용 및 이미지 동적 생성하기

1. NSMutableArray를 헤더파일에 등록시킨다.

@interface MainViewController {

NSMutableArray *arrayImages;

}


@property(retain, nonatomic) NSMutableArray *arrayImages;


@end





2. 헤더파일에 등록된 array를 .m 파일에 사용한다.


@implementation MainViewController


@synthesize arrayImages; // 등록


// 프로그램 시작점

- (void)viewDidLoad {

[super viewDidLoad];


[self.view setBackgroundColor:[UIColor blackColor]]; // 배경 셋팅하기

arrayImages = [[NSMutableArray alloc] initWithCapacity:5]; // 배열 초기화하기 (크기:5)

for( int i=0; i<5; i++)

{

  

  // 트랜스폼 객체 생성

CGAffineTransform scaleTransform = CGAffineTransformMakeScale(2, 2); 

  // 이미지 생성

UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"a%d.gif",i]]; 


   // UIImage를 UIImageView에 넣어야 이미지가 보인다.

  



UIImageView *iv = [[UIImageView allocinitWithImage:image];

  

// 메인에 넣기 (플래쉬의 stage.addChild와 같은 의미이다.)

        [self.view addSubview:ivSquare]; 

// 이미지의 알파, 센터, 크기를 조절할 수 있다.

   iv.alpha = 0;

   iv.center = CGPointMake(RANDOM_INT(0,WIDTH), RANDOM_INT(0,HEIGHT));

   iv.transform = scaleTransform;

  // 이미지를 배열에 넣는다. 나중에 꺼내서 사용할 수 있다.

         [arraySquareImages addObject:iv];

  // 사용한 메모리를 해제해야한다. 메소드 이름이 alloc 또는 copy로 시작하는 경우 반드시 메모리를 해제해야한다.

[iv release];

}

}

// 메모리 해제

- (void)dealloc {

[arrayCircleImages release];

    [super dealloc];

}



@end