I have to 20-25 download images of 50 Kb- 2 Mb each and show them in a tableview. I used ASIHTTPRequest asyn request to this. I observed that after some time the app gets stuck. This should not happen because I am using a async call. I thought something is wrong with ASIHTTPRequest and I observed that The didFinished selector gets called in the main thread. The only thing which I do is
-(void)didFinishedDownloadingImage:(ASIHTTPRequest*)request { NSData *responseData = [request responseData]; UIImage *image = [UIImage imageWithData:responseData]; [[data objectAtIndex:request.tag] setImage:image]; [self.tableView reloadData]; }
I don't think this should cause any problem. Also in cellforrowatindexpath I just do
- (UItableViewCell *)tableviewView:(UItableView *)tableview cellForItemAtIndexPath:(NSIndexPath *)indexPath { UserProfile * user = [data objecAtIndex:indexpath.row]; UITableViewCell *cell = [tableView dequeueReusableCellWithReuseIdentifier:@"ProfileCell" forIndexPath:indexPath]; if(cell == nil){ cell = [[UITableViewCell alloc] initWithStyle:UITableViewDefaultStyle]; } NSString *fullname = [NSString stringWithFormat:@"%@\n%@", user.firstname, user.lastname]; if(user.image != nil) [cell.imageView setImage:user.image]; else{ [cell.imageView setImage:[UIImage imageNamed:@"placeholder.jpg"]]; } [cell.label setText:fullname]; return cell; }
But the app is slow and freezes for 1-2 sec which is a considerable amount of time. I have seen apps which does this very smoothly. I tried using an image of fixed size 5Kb which has a very significance performance improvement with using the above code. I don't know why should that make a difference for big images in this case because all downloading is happening in other thread via ASIHTTP .
[self.tableView reloadData]
is a very expensive operation. Selectively reload just the cell that you need to. – Jack Lawrence