0

array of images added into Project

imaging is the UIImageView and imagg is Image choosen by user its default _imaging.image=imagg;

    arr = [NSArray arrayWithObjects:
                        [UIImage imageNamed:@"1.jpg"],
                        [UIImage imageNamed:@"2.jpeg"],
                        [UIImage imageNamed:@"3.jpeg"],
                        [UIImage imageNamed:@"4.jpeg"],
                        [UIImage imageNamed:@"5.jpeg"],
              nil];

assume user selected third image i.e 3.jpeg i am showing the 3.jpeg in imageview. Then using two button actions respectively Next and Previous next or previous images 3.jpeg should display.

   - (IBAction)previous:(id)sender {

    ////

    }

- (IBAction)next:(id)sender {

 //////
 }

}

for the next action its starting from 2nd image even for previous action its starting from 1st image.

2
  • problem is at button action Commented Jul 21, 2014 at 12:14
  • 1
    Just guessing: In your next/previous methods, try sending an integer value for the currentIndex +/- 1 respectively. Commented Jul 21, 2014 at 12:18

4 Answers 4

1

this is the simple concept, this is working in static method, but working fine

assign the one global variable

@property (strong,nonatomic) NSString *imagestr;


- (IBAction)previous:(id)sender {


int str=[imagestr integerValue];

NSLog(@"the int value==%d",str);
switch (str) {
    case 0:
        _imaging.image=[arr objectAtIndex:4];
        imagestr=@"4";
        break;
    case 1:
        _imaging.image=[arr objectAtIndex:0];
        imagestr=@"0";
        break;
    case 2:
        _imaging.image=[arr objectAtIndex:1];
        imagestr=@"1";
        break;
    case 3:
        _imaging.image=[arr objectAtIndex:2];
        imagestr=@"2";
        break;
    case 4:
        _imaging.image=[arr objectAtIndex:3];
        imagestr=@"3";
        break;


    default:
        break;

}
}

- (IBAction)next:(id)sender {



int str=[imagestr integerValue];

NSLog(@"the int value==%d",str);
switch (str) {
    case 0:
        _imaging.image=[arr objectAtIndex:1];
        imagestr=@"1";
        break;
    case 1:
        _imaging.image=[arr objectAtIndex:2];
         imagestr=@"2";
        break;
    case 2:
        _imaging.image=[arr objectAtIndex:3];
         imagestr=@"3";
        break;
    case 3:
        _imaging.image=[arr objectAtIndex:4];
         imagestr=@"4";
        break;
    case 4:
        _imaging.image=[arr objectAtIndex:0];
         imagestr=@"0";
        break;


    default:
        break;
}



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

1 Comment

If in array increase images then your code will work or not ?
1

On clicked on next button

Before using this first set currentIndex in this

-(IBAction)next{
    if (arr.count > 0) {
        currentIndex++;
        if (currentIndex > arr.count - 1) {
            currentIndex = 0;
        }
    imageViewObj.image = [arr objectAtIndex:currentIndex];
    }
}

on pervious button

-(IBAction)pervious{
        if (arr.count > 0) {
            currentIndex--;
            if (currentIndex <= 0) {
                currentIndex = arr.count - 1;
            }
        imageViewObj.image = [arr objectAtIndex:currentIndex];
        }
 }

Try this is working fine

3 Comments

stucked at currentIndex @joge
it's a global integer type variable and insert default index in this on "ViewDidLoad"
ya got it .. ur answer is very close to my question
0
  1. Set some integer property currentIndex to 2 (if you load first the 3rd image).
  2. When calling the next/previous methods increment/decrement that property and load the correct image. ImageView *image = [arr objectAtIndex:currentIndex]

Comments

0

when you initialize your view controller, keep track of the current image being viewed for example 0, to be used as an array index.

When next is pressed, increment the index and retrieve the image from the array using the updated count.

e.g.

 - (IBAction)next:(id)sender { 
imageCount++;
[myImageView setImage:[UIImage imageNamed:arr[imageCount] ];
 }

hope this helps. :)

Comments

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.