Lazy loading images in a UITableView

I have 50 user cells in my UITableView . I want to display the image and label in the cells where I get the images from the urls.

I want to do lazy loading of images, so the user interface does not freeze while loading images. I tried to get the images in separate streams, but I have to upload every image every time the cell becomes visible again (otherwise, reusing cells shows old images). Can someone please tell me how to duplicate this behavior.

+58
ios objective-c iphone uitableview
Jul 15 '09 at 8:21
source share
12 answers

SDWebImage Photos

The README section tells how to use it in your application.

+85
May 04 '11 at 21:23
source share

There is a good official example here from Apple: LazyTableImages I think you will find the right approach there ....

+26
Dec 01 '09 at 21:26
source share

Try the AFNetworking class, download this class from this link https://github.com/AFNetworking/AFNetworking . Add the entire AFNetworking class to the project. Then just import this category

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

Then in cellForRowAtIndexPath: as shown 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; } 

Upload your image for the image in the UITableviewcell window asynchronously. It will not load again and again while the user views the Tableview, because it also has a cache. Once the image has loaded, save the image using the key of your image. I hope this will be helpful to you.

+16
Oct 24 '13 at 4:13
source share

I created a free library designed specifically for this ... HJCache. It is very easy to use. iphone-image-cache

+8
Jan 31 '11 at 1:18
source share

I suggest switching to NSOperation and doing everything you need in a different thread.

This is the class I wrote to upload the image:

 - (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 this helps!

+3
Dec 22 '09 at 20:02
source share

You can also try EGOImageLoading . There is also a demo that shows how to use it.

+2
Jan 08 2018-11-11T00:
source share

you can use the ego picture button. You can upload ego image button files from github ... add to your project ....

change the "ego image button" class on the image in your xib ...

Lazy loading is called a synchronous request.

The image of the ego is called asynchronous request. ego image without waiting for an answer .. display all images in one go ..

+2
Sep 16 '14 at 8:09
source share

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

  • Support local and cache memory;
  • Support place holders;
  • Support touch touch (target action);
  • Support angle for image presentation;

There is also a good demonstration.

+1
May 03 '13 at 14:57
source share

you can try lazyTableImages ,




I configured lazyTableImages this project in a very simple ( customizeLazyTableImages ) and deleted all the additional codes using some static URLs and headers, only this upload images very smoothly and cache them




+1
Jun 22 '14 at
source share

I think there is another way to solve this problem if you want to load this image and then at the beginning of the view, which means

when loading -(void)loadView , then just select these images and transfer them to nsarray now that the table cell is loading, then make a look in the table cell and according to indexPath.row just replace these nsarray images with this view as a background image or subtitle these images in this new table view using the nsarray index and indexPath.row cell.

0
Oct 27 2018-11-11T00:
source share

You can use the NSOperation or GCD queue to download images in the background.

If you do not want to download images again and again, you can use NSCache or the file system to store images.

0
Jun 10 '16 at 4:05
source share

I don’t know a single built-in way to accomplish this, but it looks like you already have something working with another thread.

Assuming your only remaining problem is now invalid for the contents of the cell when it returns to view, you should look:

 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 

By implementing this delegate method, you can check the contents of a cell before drawing it to see if new content is needed. If this happens, you can clear the contents of the cells and restart the download again.

You may also consider hosting some code.

-one
Jul 15 '09 at 23:43
source share



All Articles