Kunal Balani
Kunal Balani

Reputation: 4789

downloading image is slow even after using Async call in iphone

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 .

Upvotes: 2

Views: 1117

Answers (2)

Jack Lawrence
Jack Lawrence

Reputation: 10772

It's easy to make assumptions about the root cause of a laggy/slow application. Instead of guessing, why don't you test your suspicions? Profile your application with the Time Profiler instrument. It'll tell you which methods and functions your application is spending the most time in.

Here are some ideas until you have a chance to profile:

You might consider downloading the full-res images and creating thumbnails in the background and then caching them in an NSCache object. You can also run [UIImage imageWithData:responseData]; in a background thread. It's thread-safe until the point at which it interacts with the view hierarchy.

Selectively reloading a single cell should be faster than reloading the entire tableview, especially one with lots of images. Furthermore if you're doing all of the networking and processing on a background queue, there's no reason scrolling the tableview should be slow. Can you show us your entire implementation of the -cellForRowAtIndexPath: method? You've mentioned that you think setImage: is your slow point because rendering is slow. If you reload a single cell, only one cell needs to be rendered. If you reload the entire tableview, every cell must be re-rendered.

Upvotes: 1

TonyMkenu
TonyMkenu

Reputation: 7667

Please, replace your framework with AFNetworking.

You can simple use..

IImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
[imageView setImageWithURL:[NSURL URLWithString:@"http://i.imgur.com/r4uwx.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder-avatar"]];

or... directly in TableViewCell

NSURL *url = [[NSURL alloc] initWithString:[movie objectForKey:@"artworkUrl100"]];
[cell.imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder"]];

"In the second line, we tell the image view where the thumbnail is located by passing an NSURL and we pass in a placeholder image, which is shown as long as our request has not returned a response"

Thats all!

Here you have an tutorial about that http://mobile.tutsplus.com/tutorials/iphone/ios-sdk_afnetworking/

Upvotes: 2

Related Questions