0

I have problem I am loading UITableViewCell with images all images shown to me but the problem is UTableView scroll very slow this is the code

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

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

    searchobjectval =[self.arrSearchResult objectAtIndex:indexPath.row];
    cell.location.text =searchobjectval.location;
    cell.title.text = searchobjectval.title;
    cell.cost.text = searchobjectval.roomCost;
    cell.desc.text =searchobjectval.description;

    [tableView setSeparatorColor:[UIColor blueColor]];

    UIView *myView = [[UIView alloc] init];
    if (indexPath.row % 2)
    {
        UIColor* clr = [UIColor colorWithRed:0.4f green:0.6f blue:0.8f alpha:1];
        myView.backgroundColor = clr;
    }
    else
    {
        myView.backgroundColor = [UIColor whiteColor];
    }
    cell.backgroundView = myView;

    NSLog(@" search object image %@",searchobjectval.imgLink);

    UIImage *ret = [self imageNamed:[NSString stringWithFormat: @"http://192.95.76.78/bedspace/new_arrivals_img/%@",searchobjectval.idVal ] cache:YES andsearchObj:searchobjectval];

    cell.imgVw.image = ret;

    return cell;
}

    // images caching in dictionary
    - (UIImage*)imageNamed:(NSString*)imageNamed cache:(BOOL)cache andsearchObj:(clsSearch *)searchObj
    {
         UIImage* retImage = [staticImageDictionary objectForKey:imageNamed];
        @try
        {
            if (retImage == nil)
            {
                retImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageNamed]]];
                if (cache)
                {
                    if (staticImageDictionary == nil)
                        staticImageDictionary = [NSMutableDictionary new];

                    [staticImageDictionary setObject:retImage forKey:imageNamed];

                }               
            }
        }
        @catch (NSException *exception)
        {
            NSLog(@"exception %@",exception);
        }
        @finally {
            NSLog(@"finally block execute here");
        }

        return retImage;
    }

Kindly tell me how I can fix this problem, I had paste the code inside if (cell == nil) but not successfull yet.

Girish
  • 4,692
  • 4
  • 35
  • 55
adnan
  • 21
  • 3

5 Answers5

0

The problem is the synchronous loading of images into the tablecell.Make it asynchronous and it will work fine

Use this

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
 dispatch_async(queue, ^{
       NSString *strURL = url here;
       NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
       UIImage *image = nil;
       if(data)
        image = [UIImage imageWithData:data];
      dispatch_sync(dispatch_get_main_queue(), ^{
           //now use image in image View  or anywhere according to your requirement.
           if(image)
             yourImgView = image
      });
 });
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
0

you use SDWebImage to load image from webserver.

rptwsthi
  • 10,094
  • 10
  • 68
  • 109
hitesh
  • 374
  • 1
  • 7
0

retImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageNamed]]];

for every cell appears, this line starts fetching data (image) from the server. As well as you start scrolling the UITable, the request will go to the server to get the image, so you should grab images from the server using NSThread.

Try my code here, upvote if it is useful. How to retrieve images from server asynchronously

Community
  • 1
  • 1
Vaibhav Saran
  • 12,848
  • 3
  • 65
  • 75
0

Please note that the method: [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageNamed]]]; runs on the main thread.

Refer NSOperatioQueue Samople Code or ASIHTTP library for downloading images asynchronously.

Abhishek Bedi
  • 5,205
  • 2
  • 36
  • 62
0

You can use SDWebImage library which not only loads the images asynchronusly but also makes your tableView scrolling very good and fast.

you can get the library from here.. https://github.com/nicholjs/SDWebImage-with-loading-progress

Ahmed Z.
  • 2,329
  • 23
  • 52