Multi-image download with IOS multithreading (ii) _ios

Source: Internet
Author: User

On the article to introduce you to the iOS multi-threaded implementation of multiple pictures download 1, this article continues to introduce you to the iOS multi-threaded download pictures.

This is the use of multithreading to download and store pictures, and considering the failure of the download, placeholder picture problem (the first is the download failed picture)

Gossip less, on the code, because there is a part of the same as last time, so here only upload a different

First to show you the effect of the picture:

It's still in the VIEWCONTROLLER.M.

1.

@interface Viewcontroller ()
//All Data
@property (nonatomic,strong) Nsarray *apps;
Memory Cache picture
@property (nonatomic,strong) nsmutabledictionary *imgcache;
/** All operations * *
@property (nonatomic,strong) nsmutabledictionary *operations;
/** Queue Object *
/@property (Nonatomic,strong) Nsoperationqueue *queue;

The first two coincide with the preceding

Operations use to store a dictionary of thread actions to download pictures, the main purpose is to prevent duplicate downloads

Queue is the one that is used when using multiple threads

2.

-(Nsoperationqueue *) Queue {
if (!_queue) {
_queue = [[Nsoperationqueue alloc] init];
Maximum concurrent number
_queue.maxconcurrentoperationcount = 3;
}
return _queue;

Initialization of the queue and control of child threads up to 3

3.

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath {static
NSString *id = @ "App";
UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:id];
Ddzapp *app = Self.apps[indexpath.row];
Cell.textLabel.text = App.name;
Cell.detailTextLabel.text = App.download;
First remove the picture from memory uiimage *image = Self.imgcache[app.icon]; if (image) {cell.imageView.image = image;} else {//memory no picture//write picture file data to sandbox nsstring *cachespath = [Nssearchpathfordirectoriesindomains (Nscachesdirectory,
Nsuserdomainmask, YES) firstobject];
Get filename NSString *filename = [App.icon lastpathcomponent];
Calculates the full path of the file NSString *file = [Cachespath stringbyappendingpathcomponent:filename];
Loading sandbox file data nsdata *data = [NSData datawithcontentsoffile:file]; Determine if the sandbox has picture if (data) {//Directly load sandbox picture uiimage *image = [UIImage imagewithdata:data]; cell.imageView.image = image;//Save to Dictionary (inside
In Self.imgcache[app.icon] = image; }else {///download Pictures//placeholder pictures cell.imageView.image = [UIImage imagenamed:@ "Place.jpg"];
First determine whether there is a download task//Load failure can be repeated download nsoperation *operation = Self.operations[app.icon]; if (operation = = nil) {//This picture does not download the task operation = [Nsblockoperation blockoperationwithblock:^{nsdata the *data = [NSData Dataw
Ithcontentsofurl:[nsurl URLWithString:app.icon]];
Data Load Failure if (information = = nil) {//removal operation [Self.operations RemoveObjectForKey:app.icon];
UIImage *image = [UIImage imagewithdata:data];
Save in memory self.imgcache[app.icon] = image; Back to main thread display picture [[Nsoperationqueue Mainqueue] addoperationwithblock:^{//Recurring placeholder problem//cell.imageview.image = image;//
Simply find the line where the picture is located [TableView Reloadrowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationnone];
}];
Write the picture data to the sandbox [data writetofile:file atomically:yes];
Removal operation [Self.operations RemoveObjectForKey:app.icon];
}];
Add to download queue [Self.queue addoperation:operation];
Add to dictionary self.operations[app.icon] = operation;
}} return cell; }

The method of binding the data is a bit much, because the details are considered, but the logic is similar to the last one.

About this article to introduce you to the iOS multi-threaded implementation of multiple pictures download 2, small series to introduce to everyone here, I hope to help you!

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.