Display array of Images in UIImageView

喜你入骨 提交于 2019-12-13 10:22:26

问题


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.


回答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;
}



 }



回答2:


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:


  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]



回答4:


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. :)



来源:https://stackoverflow.com/questions/24864821/display-array-of-images-in-uiimageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!