views:

452

answers:

4

i'm not understanding where in my code i write in the image names and in what order to appear. Here is my code

// load all the images from our bundle and add them to the scroll view
NSUInteger i;
for (i = 1; i <= kNumImages; i++)
{
 NSString *imageName = [NSString stringWithFormat:@"image%d.jpg", i];
 UIImage *image = [UIImage imageNamed:imageName];
 UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

I have images that are titled "image0.jpeg, image1.jpg." how to i insert this into my code and order them in a certain way?

A: 

Your code snippet is already doing what you want - at least partially.

If you have an image numbered 0, then you need to start your loop with i = 0 instea of i = 1, and adjust the constraint appropriately:

for (NSUInteger i = 0; i < kNumImages; i++) {
    NSString *imageName = [NSString stringWithFormat:@"image%u.jpg", i];

    // create the image and insert into imageview
    // add imageview to the containing view
}

The order is quite straight forward, as the images will be added 0, 1, 2.. and so forth

Jacob H. Hansen
A: 

So once I fix that code up there...how do i actually add the second and the third image into the code? i'm just a real newbie

Dane
A: 

With the code from your first post, you create multiple (kNumImages, to be more specific) ImageViews, and load in them JPG files from your project directory, called "imageN.jpg", where N is integer between 1 and kNumImages.

To display these newly created views in your UIScrollView, you have to add them to it as subviews.

Something like

for (int i = 0; i < pageCount; i++) {
 UIView *view = [pageViews objectAtIndex:i];
 if (!view.superview)
  [scrollView addSubview:view];
 view.frame = CGRectMake(pageSize.width * i, 0, pageSize.width, pageSize.height);
}

The most straightforward way to do this is in UIScrollView's UIViewController. You may want to add them in some sort of collection (it's likely that the collection retains them, so don't forget to release the views, when you add them).

As you get more comfortable with your application, you may want to lazy-load the views or use simple UIViewControllers for the different images.

In iPhone OS 3.0 and above, there are significant improvements in UIScrollView, and you can find excellent code samples in the ScrollViewSuite tutorial project on ADC's iPhone section.

Dimitar Dimitrov