Jonathan
Jonathan

Reputation: 507

Adding individual UIImageViews for each item in an array

I am trying to add a individual UiImageView for each item in an array this is what I have so far

_shapeArray = [NSArray arrayWithObjects:@"cloud.png",@"star.png", nil];

    for (int i = 0; i < [_shapeArray count]; i++){
        // I make a UIImage, which I will draw later
        UIImage* image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[_shapeArray objectAtIndex:i]]];

        UIImageView* blockView = [[UIImageView alloc] initWithImage:image];

        blockView.frame = CGRectMake(arc4random()%320, arc4random()%460, image.size.width, image.size.height);

        [self.view addSubview:blockView];

    }

But as you can tell it just adds the last image in the array. I can not figure out a way to maybe add the array object number to the name of the UIImageView. Maybe I am going at it the wrong way, if so what would be the best way?

Upvotes: 0

Views: 233

Answers (2)

Rose
Rose

Reputation: 437

You are adding the images in the same frame.

   blockView.frame = CGRectMake(arc4random()%320+SomeXValue, arc4random()%460+SomeYvalue, image.size.width, image.size.height);

Upvotes: 1

JP Hribovsek
JP Hribovsek

Reputation: 6707

This code works, but you need to make sure of a couple of things:
- That the file name actually exists in your bundle (check for uppercase/lowercase), you would not get an error message if it didn't, but it would not show the picture
- That the image sizes are not too large and don't cover each other

Upvotes: 1

Related Questions