Multi-threaded Multi-image download 1. multi-threaded image download

Source: Internet
Author: User

Multi-threaded Multi-image download 1. multi-threaded image download

The results are as follows:

 

You can see that this interface is very simple. It is actually the layout of UITableView,

However, the difficulty lies in how to download these images from the Internet and how to store them after downloading them!

 

We perform resolution step by step and download multiple images from a single thread (main thread ).

The text and image addresses on our layout are read from the plist file.

According to the structure, we customize a data model file

DDZApp. h

# Import <Foundation/Foundation. h> @ interface DDZApp: NSObject // icon @ property (nonatomic, strong) NSString * icon; // name @ property (nonatomic, strong) NSString * name; // download @ property (nonatomic, strong) NSString * download; + (instancetype) appWithDict :( NSDictionary *) dict; @ end

DDZApp. m

#import "DDZApp.h"@implementation DDZApp+ (instancetype)appWithDict:(NSDictionary *)dict {    DDZApp *app = [[self alloc] init];    [app setValuesForKeysWithDictionary:dict];    return app;}@end

The following are the code in the View Controller.

ViewController. m

1.

@ Interface ViewController () // all data @ property (nonatomic, strong) NSArray * apps; // memory cache image @ property (nonatomic, strong) NSMutableDictionary * imgCache; @ end

The first attribute is used to store and read the content in the plist file. If it is set to an attribute, you do not need to read it again.

The second attribute is used to save the images downloaded from the Internet, so that you do not need to read them repeatedly.

2.

@ Implementation ViewController // read data-(NSArray *) apps {if (! _ Apps) {// read data from the plist file NSArray * dictArray = [NSArray arrayWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ "apps. plist "ofType: nil]; NSMutableArray * appArray = [partition array]; for (NSDictionary * dict in dictArray) {[appArray addObject: [DDZApp appWithDict: dict];} _ apps = appArray;} return _ apps;} // cache image-(NSMutableDictionary *) imgCache {if (! _ ImgCache) {// initialize _ imgCache = [NSMutableDictionary dictionary];} return _ imgCache ;}

Both methods are used to initialize the two attributes.

3.

# Pragma mark-data source method-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {return self. apps. count;}-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {static NSString * ID = @ "app"; UITableViewCell * cell = [tableView preview: ID]; DDZApp * app = self. apps [indexPath. row]; cell. textLabel. text = app. name; cell. detailTextLabel. text = app. download; // retrieve the image UIImage * image = self from the memory first. imgCache [app. icon]; if (image) {cell. imageView. image = image;} else {// no image in memory // write the image file data to the sandbox NSString * cachesPath = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES) firstObject]; // obtain the file name NSString * filename = [app. icon lastPathComponent]; // calculate the full path of the file NSString * file = [cachesPath stringByAppendingPathComponent: filename]; // load the file data in the sandbox NSData * data = [NSData dataWithContentsOfFile: file]; // determine whether there is any image in the sandbox if (data) {// directly load the image cell in the sandbox. imageView. image = [UIImage imageWithData: data]; // save it to the dictionary (memory) self. imgCache [app. icon] = cell. imageView. image;} else {// download image data = [NSData dataWithContentsOfURL: [NSURL URLWithString: app. icon]; cell. imageView. image = [UIImage imageWithData: data]; // save to memory self. imgCache [app. icon] = cell. imageView. image; // write image data to the sandbox [data writeToFile: file atomically: YES] ;}} return cell ;}View Code

The two methods must be implemented by UITableView.

The first one is the returned data volume. There is nothing to say.

The second is to bind data.

 

For specific procedures, see

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.