Rob Smythe
Rob Smythe

Reputation: 429

loading images from an array of names into a series of imageviews at runtime

I have UIImageViews image1, image2, image3 etc. I have the filenames for the images to go in them in an array called imageFiles. (The images change during the program, so I can't preload them.)

Can I use a loop to load the image filenames into the series of images? (I know the code for setting an image into a specific imageView, but can I create an array of UIImageViews and load the filename at index 1 of the imageFiles array into image1, the file at index 2 into image2, and so on?

I'm new to Objective C, learning from books, websites, and tutorials as fast as I can.

Edit: I found a partial solution and added it as an answer, but I need to access the array from other methods, like touchMove. I asked this in a subsequent question..

Upvotes: 0

Views: 1201

Answers (1)

Rob Smythe
Rob Smythe

Reputation: 429

I have an answer to my own question, for anyone who is also looking for the same thing (but the solution leads to another problem!)

UIImageView *arrayOfImages[10];

int i;

for (i=0; i<10; i++){

  arrayOfImages[i]=[[UIImageView alloc] initWithImage:[UIIMage imageNamed:@"xx.png"]] autorelease];

  arrayOfImages[i].frame = CGRectMake(20,20,80,80);

  arrayOfImages[i].center = CGPointMake(100*(i+1),100);   // puts them across a horizontal row

  [self.view addSubview:arrayOfImages[i]];

}

(To get the images from another array, replace the image name with [filenameArray objectAtIndex[i]] )

But this method defined the arrayOfImages locally, and I need to access the array from other methods, like touchMove.

Upvotes: 1

Related Questions