Aslam
Aslam

Reputation: 231

How to download and display images in batch rather than downloading all in one go

I am new to iPhone programming.Using below code I am able to fetch images form server quickly and asynchronously in background.

I want to download images in batch of 15 images. Once user scrolls down he can download and see next 15 images. Also I want to unload and remove images which are no longer being displayed. So I want to download images in batches say if there are 1000 image I want them to download as 0-15, 15-30, 30-45,.. so on.

How can I implement this?

And using Lazyloading or SDWebimage source code project i can able to fetch images like what i told but i want to display images in 3*3 thumbnail(means each row i wnat to display 3 images) Please tell me how can i do like this.I want some lib like Universal library in Android

  //remote image URLs
        URLs = [[NSMutableArray alloc]init];

              for (NSString *path in latestiamge)
        {

            NSURL *URL = [NSURL URLWithString:path];
            if (URL)
            {
                [URLs addObject:URL];

            }
            else
            {
            }
        }

        self.imageURLs = URLs;

        UISwipeGestureRecognizer* swipeUpGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeUpFrom:)];
        swipeUpGestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
        [myScrollView addGestureRecognizer:swipeUpGestureRecognizer];

        float horizontal = 2.0;
        float vertical = 2.0;

        for(int i=0; i<[imageURLs count]; i++)

        {
            if((i%3) == 0 && i!=0)
            {
                horizontal = 5.0;
                vertical = vertical + 100.0 + 5.0;
            }
            CGRect frame;
            frame.size.width=100.0;
            frame.size.height=100.0;
            frame.origin.x=0;
            frame.origin.y=0;
            AsyncImageView *imageView = [[AsyncImageView alloc] initWithFrame:frame];
            imageView.contentMode = UIViewContentModeScaleAspectFill;
            imageView.clipsToBounds = YES;
            imageView.tag = i;
            //load the image
            imageView.imageURL = [imageURLs objectAtIndex:i];

            UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]
                                                 initWithTarget:self
                                                 action:@selector(actionHandleTapOnImageView:)];
            imageView.userInteractionEnabled = YES;
            [imageView addGestureRecognizer:singleTap];
            [myScrollView addSubview:imageView];

            buttonImage1 = [UIButton buttonWithType:UIButtonTypeCustom];
            [buttonImage1 setFrame:CGRectMake(horizontal, vertical, 100.0, 100.0)];
            [buttonImage1 setTag:i];

            [buttonImage1 setImage:[idURLs objectAtIndex:i] forState:UIControlStateNormal];
            [buttonImage1 setImage:[UIImage imageNamed:@"Overlay.png"] forState:UIControlStateSelected];


            [myScrollView addSubview:buttonImage];

            [buttonImage1 addSubview:imageView];
            [myScrollView addSubview:buttonImage1];


           horizontal = horizontal + 100.0 + 5.0;

        }
        [myScrollView setContentSize:CGSizeMake(320.0, vertical + 3900.0)];

Upvotes: 3

Views: 699

Answers (1)

devluv
devluv

Reputation: 2017

If you are interested in learning the concepts behind all the working.. Please go through these for basic understanding:

http://www.raywenderlich.com/4295/multithreading-and-grand-central-dispatch-on-ios-for-beginners-tutorial

http://www.raywenderlich.com/19788/how-to-use-nsoperations-and-nsoperationqueues

And if you just want to use asynchronous loading. I suggest you to use this. It is based on UIImage. It is very good

https://github.com/rs/SDWebImage.git

I suggest you to look into all of these and have fun.

Upvotes: 4

Related Questions