I need to create a certain number of UIImageViews based on the amount of objects inside of an NSMutableArray. For example, if there are 5 UIImage's inside of this NSMutableArray, how do I create 5 UIImageView's with those images?
Thanks
Use count property of NSArray:
UIImage *image1 = [UIImage imageNamed:@"img1.png"];
UIImage *image2 = [UIImage imageNamed:@"img2.png"];
UIImage *image3 = [UIImage imageNamed:@"img3.png"];
UIImage *image4 = [UIImage imageNamed:@"img4.png"];
UIImage *image5 = [UIImage imageNamed:@"img5.png"];
NSArray *imageArray = @[image1, image2, image3, image4, image5];
NSInteger imageCount = 0;
NSInteger imageViewYM = 20; // Ensure this is bigger than image height
for (UIImage *image in imageArray) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, imageViewYM * imageCount, image.size.width, image.size.height)];
imageView.image = image;
[self.view addSubview:imageView];
imageCount++;
}
Run a while loop for all the images and create a new UIImageView for each of them:
NSArray *imageArray = @[image1, image2, image3];
int i = 0;
while(i < imageArray.count) {
UIImageView * imageView = [[UIImageView alloc] init];
imageView.image = [imageArray objectAtIndex:i];
imageView.frame = CGRectMake(0, 0, 50, 50);
imageView.tag = i;
[self.view addSubview:imageView];
i++;
}