-4

i am getting data from server that contains some images i get properly from url and set it to the tablview cell correctly done.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AppDelegate * delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];

    static NSString *CellIdentifier = @"Cell";
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text=[delegate.firstArray objectAtIndex:indexPath.row];

    NSLog(@"%@",delegate.thirdArray);

    url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",[delegate.thirdArray objectAtIndex:indexPath.row]]];

    data = [[NSData alloc]initWithContentsOfURL:url];

    UIImage * img=[UIImage imageWithData:data];

    cell.imageView.image=img;

    return cell;
}

in delegate.thirdArray contais this url

 "http://awe.com/app/images/can.png",
"http://awe.com/app/images/card.png",
"http://awe.com/app/images/tp.png",
"http://awe.com/app/images/tricks.png" 

images load properly but it will take some time to load that image and scroll the tableview it will very slow i want it to be fast how may i do this.

Master Stroke
  • 5,108
  • 2
  • 26
  • 57
Jitendra
  • 5,055
  • 2
  • 22
  • 42

4 Answers4

3

Load your images using [NSURLConnection sendAsynchronousRequest:queue:completionHandler: then use NSCache to prevent downloading the same image again and again.

As suggested by many developers go for SDWebimage and it does include the above strategy to download the images files .You can load as many images you want and the same URL won't be downloaded several times as per the author of the code

Example on [NSURLConnection sendAsynchronousRequest:queue:completionHandler:

NSURL *url = [NSURL URLWithString:@"your_URL"];
NSURLRequest *myUrlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest: myUrlRequest queue: queue completionHandler: ^ (NSURLResponse *response, NSData *data, NSError *error)
{

    if ([data length] > 0 && error == nil)
        //doSomething With The data

    else if (error != nil && error.code == ERROR_CODE_TIMEOUT)
        //time out error

    else if (error != nil)
        //download error
}];

Then use NSCache...

For further explanation: READ HERE

Community
  • 1
  • 1
Master Stroke
  • 5,108
  • 2
  • 26
  • 57
1

you have to load images asynchronously so tableview can load fast here in your code tableview take so much time because it load image for every cell.

Please check below answer you can find how to load images asynchronously.

https://stackoverflow.com/a/15377082/1713478

Please change url with your url

hope your problem will solved.

Community
  • 1
  • 1
Pratik
  • 2,399
  • 17
  • 36
0

Download AFNetworking from this link

https://github.com/AFNetworking/AFNetworking

and then add files to your project and then in .m file

#import "UIImageView+AFNetworking.h"


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AppDelegate * delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];

    static NSString *CellIdentifier = @"Cell";
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text=[delegate.firstArray objectAtIndex:indexPath.row];

    NSLog(@"%@",delegate.thirdArray);

    url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",[delegate.thirdArray objectAtIndex:indexPath.row]]];



    [cell.imageView setImageWithURL:url];

    return cell;
}
h.kishan
  • 681
  • 6
  • 20
0

Another answer is to use the amazing SDWebImage framework. It will load your images asynchronously and cache them so if the user has to load the same image again he won't have to download it again.

Here is the link to the framework :

https://github.com/rs/SDWebImage

Here is the code for your cell :

#import <SDWebImage/UIImageView+WebCache.h>

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    AppDelegate * delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];

    static NSString *CellIdentifier = @"Cell";
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text=[delegate.firstArray objectAtIndex:indexPath.row];

    NSLog(@"%@",delegate.thirdArray);

    url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",[delegate.thirdArray objectAtIndex:indexPath.row]]];

    [cell.imageView setImageWithURL:url];
    // [cell.imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; if you want to add a placeholder image

    return cell;
}
Ticko
  • 368
  • 1
  • 2
  • 11