Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lazy load images in UITableView

People also ask

When to use lazy loading in Swift?

Lazy loading is a design pattern commonly used in computer programming to defer initialization of an object until the point at which it is needed. It can contribute to efficiency in the program's operation if properly and appropriately used. The opposite of lazy loading is eager loading.

What is lazy loading in IOS?

Ans : Lazy Loading Images is a technique to resolve loading image from the web. The thing is that, I need to display images directly from the web in UIImageView or any other control.

How to load image from URL in tableView Swift?

To load an image in table view cell we'll go through a series of steps. Create a table view, table view cell and add an Image view to it. Assign a custom class to the cell we created. In the cell for row at method write the following lines of code.

What is the use of lazy loading?

Lazy loading is a strategy to identify resources as non-blocking (non-critical) and load these only when needed. It's a way to shorten the length of the critical rendering path, which translates into reduced page load times.


Try AFNetworking class download this class in this link https://github.com/AFNetworking/AFNetworking . Add the all AFNetworking class in your project.Then just import this category

#import "UIImageView+AFNetworking.h" in your Viewcontroller which contains your Tableview.

Then in cellForRowAtIndexPath: put like below

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if (cell == nil) 
    {
        cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }

    [cell.imageView setImageWithURL:[NSURL URLWithString:[UrlArray objectAtIndex:indexPath.row]] placeholderImage:[UIImage imageNamed:@"placeholder.jpg"]];
     cell.myLabel.text = [imageNameArray objectAtIndex:indexPath.row];

     cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;    
}

This download your Image for imageView in the UITableviewcell asynchronously. It wont download again and again while the user scroll the Tableview, because it has cache also. Once your image download it save the image with key of your imageUrl. I hope it useful for you.


I'd suggest going for an NSOperation and doing whatever you need on another thread.

This is a class I wrote for image loading:

- (id)initWithTarget:(id)trgt selector:(SEL)sel withImgURL:(NSString *)url {
    if(self = [super init]) {
        if(url == nil || [url isEqualToString:@""])
            return nil;
        target = trgt;
        action = sel;
        imgURL = [[NSURL alloc] initWithString: url];
    }
    return self;
}

- (void)main {
    [NSThread detachNewThreadSelector:@selector(loadImage) toTarget:self withObject:nil];
}

- (void)loadImage {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    UIImage *img = [UIImage imageNamed: @"default_user.png"];
    if(![[imgURL absoluteString] isEqualToString: @"0"]) {
        NSData *imgData = [NSData dataWithContentsOfURL: imgURL];
        img = [UIImage imageWithData: imgData];
    }
    if([target respondsToSelector: action])
        [target performSelectorOnMainThread: action withObject: img waitUntilDone: YES];
    [pool release];
}

- (void)dealloc {
    [imgURL release];
    [super dealloc];
}

Hope that helps!


you can use ego image button..you can download ego image button files from github...add in your project....

change the class " ego image button" at image view in your xib...

lazy loading is called synchronous request..

ego image is called asynchronous request. ego image dont wait for response..display all images at one time..


Maybe you can have a try ALImageView.It is much simpler than SDWebImage.You only need two source files(ALImageView.h/ALImageView.m).You can reuse the image view to reload different urls in a tableview cell.

  1. Support local and memory cache;
  2. Support place holders;
  3. Support tap touch(target-action);
  4. Support corner for the image view;

There is also a good demo.


you can try this lazyTableImages ,


i had customized lazyTableImages this project into a very simple one (customizeLazyTableImages ) and removed all extra codes with some static urls and titles only this is loading images very smoothly and caching them