Adding individual UIImageViews for each item in an array

微笑、不失礼 提交于 2020-01-03 05:09:09

问题


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?


回答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




回答2:


You are adding the images in the same frame.

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


来源:https://stackoverflow.com/questions/13409283/adding-individual-uiimageviews-for-each-item-in-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!