0

I was wondering about a loop to create new UIImageViews at runtime. I want each UiImageView To Have A Different Name. If the user specifies that they want 100 images, 100 images are appropriately sized an placed in a specific area of the screen. They need to fill up a jar. Similar to the Starbucks Mobile Card App stars section, except there is an unlimited about of possible images.

How would I do this?

EDIT: The Image Views Should Fit Inside The Jar Below: Jar

Thanks

1
  • 1
    If you're going to be doing this, watch your memory footprint and react to memory warnings or you will get into trouble. Only load the images for image views which you actually need to display. Commented Oct 10, 2011 at 21:12

2 Answers 2

1

You can create multiple UIImageViews using a for loop and an NSMutableArray, something like this:

NSMutableArray *imagesArray = [[NSMutableArray alloc] init];
for (int i=0; i<100; ++i) {
    UIImageView *imgView = [[UIImageView alloc] init];
    // customize the UIImageView as you like
    [imagesArray addObject:imgView];
    [imgView release];
}

Alternately, if the names are important, then create an NSMutableDictionary and use -addObject:forKey: to make the UIImageView the object for the key @"imgName" or whatever name you like.

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

6 Comments

Is there a way to to release/delete them all instantaneously?
So, If I were to use a page controller, I would just allocate the original array?
Looks like memory trouble. 100 UIImageViews with the image property set might well crash your app. You need to probably persist the data and recreate as needed.
@PengOne, Is There A Way To Keep The Images In A Certain Area of The Screen?
@AhanMalhotra put them in a UIScrollView
|
0

Bottom line on the memory issue: Don't allow an infinite number of images.

  1. If the images are filling up a jar, to be realistic you must have overlapping. There is no way in heck the user is going to be able to see one hundred different images if they are all showing onscreen at the same time.

  2. Limit the user. You need to get the concept of infinity out of your head. Then you should be fine. What you really want is for your program to display a lot of images. I'm not sure whether you intend to allow zooming or not - which would complicate things much more - but 100 images displayed across the iPhone screen at equal sizes not including the jar that you mentioned are 48 pixels by 32 pixels. That's already pretty small.

  3. We really need more information in order to advise you as to how to approach this issue. Do you intend to allow zoom? Are the images overlapping or not? how small do you intend for the minimum size to be? etc, etc.

As for the multiple image views, PengOne is correct.

This will add the images to an NSMutableArray.

NSMutableArray *imagesArray = [[NSMutableArray alloc] init];
for (int i=0; i<100; ++i) {
    UIImageView *imgView = [[UIImageView alloc] init];
    // customize the UIImageView as you like
    [imagesArray addObject:imgView];
    [imgView release];
}

(From PengOne)

You will of course have to add them to the screen with a loop and eventually remove them from it.

10 Comments

I made some changes to PengOnes code NSMutableArray *imagesArray = [[NSMutableArray alloc] init]; while (loop <= [child.marblesneeded intValue]) { UIImageView *imgView = [[UIImageView alloc] init]; loop++; [imagesArray addObject:imgView]; [imgView release]; Also, See The Edit in my question. I do not know how it would look if they overlap, but the marbles can be pretty small.
I don't really understand what the advantage of your while loop is over his for loop. That seems to be the only change you made.
How do I add the image views to the screen(or another view/scrollview)? Thanks!
[viewToAddTo addSubview:imageView];
Here is my code: `NSMutableArray *imagesArray = [[NSMutableArray alloc] init]; for (int i=0; i<10; ++i) { UIImageView *imgView = [[UIImageView alloc] init]; imgView.image = [UIImage imageNamed:@"Aqua_Transparent"]; [self.jarView addSubview:imgView]; [imagesArray addObject:imgView]; [imgView release]; } ' Nothing appears on the screen with this code. jarView is a UIScrollView. Thanks for all your help!
|

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.