0

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?

4
  • What result are you getting? Two image views, each with star.png as the image? Commented Nov 16, 2012 at 2:08
  • No only one UIImageView with the star image. Commented Nov 16, 2012 at 2:30
  • Probably cloud.png is not in your bundle. Try to delete the image, re-import it, select "Copy Item.." and flag your target in "Add to targets". Commented Nov 16, 2012 at 2:44
  • Ok thanks that weird becuase I tried it on a different iPad and it worked just fine. I had been stuck on that for a while. Ok next questions how can I add touch events to them? Commented Nov 16, 2012 at 3:50

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

You are adding the images in the same frame.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.