Objective C - UITableViewCell loading image asynchronously

前端 未结 4 422
感情败类
感情败类 2021-01-01 07:56

I am displaying a table. Each row has an image icon, loaded from an URL.

Since downloading images synchronously blocks the UI, I\'ve implemented am asynchronous way

4条回答
  •  鱼传尺愫
    2021-01-01 08:59

    Take a look at AFNetworking... They make dealing with network callbacks EASY!!! I recommend AFNetworking over ASIHTTPRequest because AFNetworking is keeping updated and ASIHTTPRequest is not... they kinda just stopped developing.

    Here is an example of how to use AFNetworking to download images asynchronously:

    NSDictionary * object = [self.array objectAtIndex:indexPath.row];
    NSDictionary * image = [object valueForKey:@"image"];
    NSString *imageUrl = [image valueForKeyPath:@"url"];
    NSURL *url = [NSURL URLWithString:imageUrl];
    [cell.imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"empty-profile-150x150.png"]];
    

    The setImageWithURL: placeholderImage: method is what I used to do this... Rather than multiple methods and lines of code, I accomplish everything with this line.

    This entire line of code does exactly what you want to do. There is really no need to recreate the wheel :). It does help though going through the lower level of programming to really understand the implementation of what is REALLY going on under the hood.

    View the link to download the library and view more examples on how to use it... Seriously, it makes your life alot easier not having to worry THAT MUCH about threads and GCD.

    AFNetworking Demo

    I haven't dived into the code of AFNetworking but my application runs like BUTTER when loading the images into the cells. It looks great :)

    Oh and here the docs for AFNetworking: AFNetworking Documentation

提交回复
热议问题