PDA

View Full Version : View Pictures




dakotas
Apr 11, 2011, 03:22 PM
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?



nickculbertson
Apr 11, 2011, 09:28 PM
do something like this.
(images is an ImageView)


-(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

dakotas
Apr 12, 2011, 04:34 AM
thanks nick i will try that :)

dejo
Apr 12, 2011, 09:50 AM
Or you could consider preloading your images into an array and then just using indexes to reference them. Something like this:

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