Kalzem
Kalzem

Reputation: 7501

Slow UITableView with Facebook Image

I have a uitableview and each cells have an image from facebook image profile and 2 uilabels (from singleton, not an internet request)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    static NSString *CellIdentifier = @"MenuCell";
    MenuCellViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[MenuCellViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    //Singleton
    DataModel *m = [DataModel getModel];
    switch ([indexPath section]) {
        case 0: //Only Header
            break;

        case 1: //My cells
        {
            //If footer - set footer background
            if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section] - 1){
               [cell setBackgroundView:[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"imgFooter.png"]]];
            //Else - set normal cell background
            } else {
                [cell setBackgroundView:[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"img.png"]]];
            }

            //Get Facebook image
            strForImg = [[NSString alloc] initWithFormat:@"https://graph.facebook.com/%@/picture?type=square",[[[m list] objectAtIndex:indexPath.row] objectForKey:@"id"]];    
            url =[NSURL URLWithString:strForImg];
            img = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
            [cell.imgProfile setImage:img];

            //Set some text (uilabels)
            cell.lblNom.text = [[[m list] objectAtIndex:indexPath.row] objectForKey:@"name"];
            cell.lblTour.text = [[[m list] objectAtIndex:indexPath.row] objectForKey:@"description"];
            break;
        }
    }
}

This code is very slow and freezes the tableview as it loads from facebook the profile images of each cell dynamically.

I tried doing with a dispatch like this

case 1://My cells
    {
        if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section] - 1){
           [cell setBackgroundView:[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"imgFooter.png"]]];
        } else {
            [cell setBackgroundView:[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"img.png"]]];
        }

        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
        dispatch_async(queue, ^{
            dispatch_sync(dispatch_get_main_queue(), ^{
                strForImg = [[NSString alloc] initWithFormat:@"https://graph.facebook.com/%@/picture?type=square",[[list objectAtIndex:indexPath.row] objectForKey:@"id"]];

                url =[NSURL URLWithString:strForImg];
                img = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
                [cell.imgProfile setImage:img];

            });
        });
        cell.lblNom.text = [[[m list] objectAtIndex:indexPath.row] objectForKey:@"name"];
        cell.lblTour.text = [[[m list] objectAtIndex:indexPath.row] objectForKey:@"description"];
        break;
    }

The speed has slighty increased but it is still FREEZING when scrolling.

Is my dispatch not correct ? (I have just discovered it, maybe it is not well used).

Or is there any better method ? I like the facebook friend picker. It puts a default profile image and refreshes the correct image profile when it is received from internet. How is this magic done ? :)

Upvotes: 0

Views: 712

Answers (1)

NeverBe
NeverBe

Reputation: 5038

Fix this block:

 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
        dispatch_async(queue, ^{
                strForImg = [[NSString alloc] initWithFormat:@"https://graph.facebook.com/%@/picture?type=square",[[list objectAtIndex:indexPath.row] objectForKey:@"id"]];

                url =[NSURL URLWithString:strForImg];
                img = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
            dispatch_sync(dispatch_get_main_queue(), ^{
                [cell.imgProfile setImage:img];

            });
        });

Upvotes: 1

Related Questions