Load images on a image view using horizontal scrolling

China☆狼群 提交于 2019-12-04 18:11:22
Bikram Thapa

So, you want to built the gallery view:

For this implement the following steps:

  1. Add the UIScrollView to the view of your view controller.

  2. In viewDidLoad method: Load the name of the images in the "ImageArray". (I assume all your images have names as "img1.png", "img2.png", "img3.png", ....)

    ImageArray=[[NSMutableArray alloc]init];
    
    for (int i=0; i<19; i++) {
       NSString *imgtext=[[NSString alloc]initWithFormat:@"img%d",i+1];
       [ImageArray addObject:imgtext];
    }
    
  3. In the viewWillAppear method, add the following code:

    for (int i = 0; i < ImageArray.count; i++) {
    CGRect frame;
    frame.origin.x = self.scrollview.frame.size.width * i;
    frame.origin.y = 0;
    frame.size = self.scrollview.frame.size;
    UIView *subview = [[UIView alloc] initWithFrame:frame];
    
    UIImage *image = [UIImage imageNamed: [NSString stringWithFormat:@"%@.png",[ImageArray objectAtIndex:i]]];
    UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
    [imageView setFrame:CGRectMake(0, 0, frame.size.width,frame.size.height )];
    [subview addSubview:imageView]; 
    [self.scrollview addSubview:subview];
     }
    
    self.scrollview.contentSize = CGSizeMake(self.scrollview.frame.size.width * ImageArray.count, self.scrollview.frame.size.height);
    
    self.scrollview.contentOffset=CGPointMake (self.scrollview.frame.size.width, 0);
    

Hope it helps.

I have made a scroll view in my app in which I have added multiple images with horizontal scrolling. Below is the function which may help you..

- (void)makeFriendsScrollView{
    int friendsCount = 10;
    float xPos = 10;
    for (int i = 0; i < friendsCount; i++) {


        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(xPos,25 , 54, 54)];
        imgView.image = [UIImage imageNamed:@"user_default.png"];
        [imgView.layer setCornerRadius:27.0];
        imgView.clipsToBounds = YES;

        [scrollViewFriends addSubview:imgView];
        xPos = xPos + 64;

    }
    scrollViewFriends.contentSize = CGSizeMake(friendsCount*65, 90);
    [scrollViewFriends.layer setBorderColor:[UIColor lightGrayColor].CGColor];
    [scrollViewFriends.layer setBorderWidth:0.5f];
}

i simply did this in view controller check it out...and change according to your requirement..try this in viewWillAppear method

self.arraname=[NSArray arrayWithObjects:@"1.jpg",@"2.jpg",@"3.jpg", nil];
//    int friendsCount = 10;
    float xPos = 160;
    float x1=0;
    float y=60;
    for (int i = 0; i < [self.arraname count]; i++)
    {

         x1=xPos+(260*i);
        _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(x1, y, 54, 54)];
        _imgView.image = [UIImage imageNamed:[self.arraname objectAtIndex:i]];
        [_imgView.layer setCornerRadius:27.0];
        _imgView.clipsToBounds = YES;

        [self.scroll addSubview:_imgView];

    }
    NSLog(@"%f",x1);
    self.scroll.contentSize=CGSizeMake(x1+200, 0);
    self.scroll.showsHorizontalScrollIndicator = YES;
    self.scroll.showsVerticalScrollIndicator=NO;
    self.scroll.pagingEnabled = YES;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!