View Pictures

Discussion in 'iPhone/iPad Programming' started by dakotas, Apr 11, 2011.

  1. macrumors newbie

    #1
    Hi, I have ten pictures and I want to have two buttons, previous and next so i can click through to view the pictures. I am finding tutorials on animation which is not what i want to achieve. can anyone help me?
     
  2. macrumors regular

    nickculbertson

    #2
    do something like this.
    (images is an ImageView)

    Code:
    -(IBAction) right{
    UIImage *Image1 = [UIImage imageNamed:@"yourpic1.png"];
    UIImage *Image2 = [UIImage imageNamed:@"yourpic2.png"];
    UIImage *Image3 = [UIImage imageNamed:@"yourpic3.png"];
    
    
    
                            if(images.image == Image1)
    				images.image = Image2;
    			else if(images.image == Image2)
    				images.image = Image3;
                            else if(images.image == Image3)
    				images.image = Image1;
    }
    
    -(IBAction) left{
    UIImage *Image1 = [UIImage imageNamed:@"yourpic1.png"];
    UIImage *Image2 = [UIImage imageNamed:@"yourpic2.png"];
    UIImage *Image3 = [UIImage imageNamed:@"yourpic3.png"];
    
    
    
                            if(images.image == Image3)
    				images.image = Image2;
    			else if(images.image == Image2)
    				images.image = Image1;
                            else if(images.image == Image1)
    				images.image = Image3;
    }
    
    
    Nick
     
  3. macrumors newbie

    #3
    thanks nick i will try that :)
     
  4. Moderator

    dejo

    Staff Member

    #4
    Or you could consider preloading your images into an array and then just using indexes to reference them. Something like this:
    Code:
    NSArray *imageArray;
    int imageArrayIndex;
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        imageArray = [[NSArray alloc] initWithObjects:
            [UIImage imageNamed:@"yourpic1.png"],
            [UIImage imageNamed:@"yourpic2.png"],
            [UIImage imageNamed:@"yourpic3.png"],
            nil];
    
        imageArrayIndex = 0;
    }
    
    - (IBAction)right {
        imageArrayIndex++;
        if (imageArrayIndex == imageArray.count) {
            imageArrayIndex = 0;
        }
        images.image = [imageArray objectAtIndex:imageArrayIndex];
    }
    
    - (IBAction)left {
        imageArrayIndex--;
        if (imageArrayIndex == -1) {
            imageArrayIndex = imageArray.count - 1;
        }
        images.image = [imageArray objectAtIndex:imageArrayIndex];
    }
    
    That way if you add or remove images from the array, the only code you'd need to change would be in viewDidLoad.

    P.S. I've left the release of imageArray as an exercise for the reader. :D
     

Share This Page