Irwan Madness
Irwan Madness

Reputation: 1396

TableView lags when Scrolling

I am trying to implement a table like facebook or twitter timeline. but when I scroll the UITableView, it's very slow and lagging. How can I fix this problem ?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ViewControllerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"htrcell"];
    if (cell==nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    NSDictionary *dataArray = [self.threndsArray objectAtIndex:indexPath.row];

    NSURL *url = [NSURL URLWithString:[dataArray objectForKey:@"pic_timeline"]];

    NSData *imgData = [NSData dataWithContentsOfURL:url];

    cell.upload_image.image =[UIImage imageWithData:imgData];

    NSURL *urlimage = [NSURL URLWithString:[dataArray objectForKey:@"pic_user"]];
    NSData *Dataimage = [NSData dataWithContentsOfURL:urlimage];
    cell.uploder_image.image =[UIImage imageWithData:Dataimage];
    cell.uploder_image.clipsToBounds = YES;
    cell.uploder_image.layer.cornerRadius = cell.uploder_image.frame.size.height /2;
    cell.uploder_image.layer.borderWidth = 2.0;
    cell.uploder_image.layer.borderColor = [UIColor whiteColor].CGColor;

    cell.uploder_name.text=[dataArray valueForKeyPath:@"nama_user"];

    cell.like_counter.text=[NSString stringWithFormat:@"%@",[dataArray valueForKeyPath:@"likes"]];

    cell.commenter_name.text=[dataArray valueForKeyPath:@"status_timeline"];

    cell.comments.text=[dataArray valueForKeyPath:@"tgl_timeline"];


    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    return  cell;
}

Upvotes: 1

Views: 858

Answers (7)

Nilesh
Nilesh

Reputation: 699

The best way is SDWebImageView Download and Use Like Bellow :

Import 2 files in your .h

#import "SDWebImage/UIImageView+WebCache.h"
#import "UIImageView+UIActivityIndicatorForSDWebImage.h"
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ViewControllerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"htrcell"];
    if (cell==nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    NSDictionary *dataArray = [self.threndsArray objectAtIndex:indexPath.row];

    NSURL *url = [NSURL URLWithString:[dataArray objectForKey:@"pic_timeline"]];

    [cell.upload_image setImageWithURL:[dataArray objectForKey:@"pic_timeline"] placeholderImage:nil usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

    cell.uploder_image.clipsToBounds = YES;
    cell.uploder_image.layer.cornerRadius =       cell.uploder_image.frame.size.height /2;
    cell.uploder_image.layer.borderWidth = 2.0;
    cell.uploder_image.layer.borderColor = [UIColor whiteColor].CGColor;


    cell.uploder_name.text=[dataArray valueForKeyPath:@"nama_user"];

    cell.like_counter.text=[NSString stringWithFormat:@"%@",[dataArray valueForKeyPath:@"likes"]];

    cell.commenter_name.text=[dataArray valueForKeyPath:@"status_timeline"];

    cell.comments.text=[dataArray valueForKeyPath:@"tgl_timeline"];


    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    return  cell;
}

Upvotes: 0

CoderSulemani
CoderSulemani

Reputation: 123

The slow loading could be because of the large data objects you have put on display. You could use some cache libraries or execute the cache provided by objective-C through NSCache class. The other thing you could do is use the ComponentKit library provided by Facebook in your project.

Caching libraries:

FastImageCache

SDWebImage

Timeline Libraries:

Facebook's Component Kit

Upvotes: 0

Kamal Thakur
Kamal Thakur

Reputation: 408

Please Try SDWebimage Library or download image Asynchronous

If you using SDWebimage please try this method:-

[imageView sd_setImageWithURL:[NSURL URLWithString:@"https://graph.facebook.com/olivier.poitrey/picture"]
             placeholderImage:[UIImage imageNamed:@"avatar-placeholder.png"]
                      options:SDWebImageRefreshCached];

Upvotes: 0

This Happens Due to The Size of image is Big. 1.Reuduce the Image Sizes to (below 200 kb) without Reducing The Quality in Backend 2.Download The images In Background

NSURLSessionDownloadTask *downloadPhotoTask = [[NSURLSession sharedSession]
                                                   downloadTaskWithURL:[NSURL URLWithString:@"Your_URL"] completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
                            UIImage *downloadedImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:location]];

                                                       dispatch_async(dispatch_get_main_queue(), ^{
                                                           downloadAllImageview.image=downloadedImage;
                                                           [activityIndicator stopAnimating];
                                                           [activityIndicator removeFromSuperview];
                                                       });

                                                   }];

    [downloadPhotoTask resume];

Upvotes: 0

Anirban iOS
Anirban iOS

Reputation: 977

The main problem is that, you are converting image url to NSData in main thread that is why the lag scrolling happens. You need to convert image url to NSData in background queue and update the UI in main queue.

That way you load each image in the background and as soon as its loaded the corresponding cell is updated on the mainThread.

The following code may help you

 NSURL *urlimage = [NSURL URLWithString:[dataArray objectForKey:@"pic_user"]];
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void) {
            NSData *Dataimage = [NSData dataWithContentsOfURL:urlimage];
            dispatch_sync(dispatch_get_main_queue(), ^(void) {
             cell.uploder_image.image =[UIImage imageWithData:Dataimage];
            });
    });

Upvotes: 1

Pradumna Patil
Pradumna Patil

Reputation: 2220

If your loading images using some URL then the best way is to load images Asynchronously. So I suggest you to check the below links to load images Asynchronously

  1. AsyncImageView
  2. SDWebImage

Upvotes: 0

Rajesh Dharani
Rajesh Dharani

Reputation: 285

You can try NSURLRequest also

NSURL *url = [NSURL URLWithString:[dataArray objectForKey:@"pic_timeline"]];

NSURLRequest *request = [NSURLRequest requestWithURL:url];

[NSURLConnection sendAsynchronousRequest:request
                 queue:[NSOperationQueue mainQueue]
                 completionHandler:^(NSURLResponse * response,
                 NSData * data, NSError * error) 
{

    if (!error) {

        cell.upload_image.image = [UIImage imageWithData:data];

    }
}

Upvotes: 0

Related Questions