Create New UiImageViews Programmatically
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 ar开发者_如何学Goe 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:
Thanks
You can create multiple UIImageView
s 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.
Bottom line on the memory issue: Don't allow an infinite number of images.
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.
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.
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.
精彩评论