Hard-working, stupid old man.
Website: http://www.jianshu.com/p/02ab2b74c451
Recently, the problem of asynchronously loading network images in Uitbleview has been studied, and iOS apps often see this interface. A tableview shows some titles, details, and so on, plus a picture. Let's talk about this idea here.
To prevent images from being downloaded multiple times, we need to cache the images and cache them in the sandbox cache, both of which we have to implement.
Since Tableviewcell is a reusable mechanism, that is, only an instance of the currently visible number of cells in memory, when sliding, a new display cell reuses the cell object that is being slid out. So there is a problem:
In general, in the Cellforrow method we will set the cell image data source, that is, if a cell ImageView object opens a download task, this time the cell object has been reused, the new image data source will open another download task , 2 download task callbacks to the same ImageView object occur because their associated ImageView object is actually the ImageView object of the same cell instance. At this point it is necessary to do some processing to avoid the wrong image data source refreshing the UI when the callback occurs.
So when we slide down the tableview we need to manually cancel the drop-down operation, when the user stops swiping and then goes to the download operation. This strategy is also used by Sdwebimage.
Very simple we only need to listen to the ScrollView proxy method (TableView inherits from ScrollView).
/**
* Called when the user starts dragging the table
*/
-(void) scrollviewwillbegindragging: (Uiscrollview *) ScrollView
{
Pause download
[Self.queue Setsuspended:yes];
}
/**
* Called when the user stops dragging the table
*/
-(void) scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate
{
Resume Download
[Self.queue Setsuspended:no];
}
and sdwebimage only use this way, so in the run Sdwebimage of the demo, you can see, if the fast slide down, and then slide back, the picture is a moment before the show, this is because the fast sliding, the old data source download task was canceled.
/-------------------------------------------
Here's a look at the specific ideas.
Download images asynchronously we use nsoperation and create a global queue to manage the operation of downloading pictures.
/**
* Queue for all download operations
*/
@property (nonatomic,strong) nsoperationqueue* queue;
Also need two dictionaries operations, images
/**
* Store all download operations (URL is key,operation object is value)
*/
@property (Nonatomic,strong) nsmutabledictionary* operations;
/**
* Store all downloaded images for memory cache, same URL as key
*/
@property (Nonatomic,strong) nsmutabledictionary* images;
Before displaying the picture on the cell:
First determine if there is a picture in memory (in the images dictionary),
If so, then take out the URL corresponding to the picture to display,
If not, then go to the sandbox cache to see, of course, stored in the sandbox is nsdata.
If there is a sandbox cache, we remove the corresponding data to the cell to display
If there is no picture in the sandbox, we first display the placeholder picture. Create operation again to perform the download operation.
Of course, before we create the operation, we need to determine if the operation operation exists.
This is the time for us to operations this dictionary.
Remove the download operation for the current URL
nsblockoperation* operation = Self.operations[app.icon];
If there is no download operation, we need to actually create operation to perform the download.
After the download operation is created, it should be stored in the global queue to execute asynchronously, while the operation is recorded in the Operations dictionary.
Adding actions to the queue
[Self.queue addoperation:operation];
Add to Dictionary
Self.operations[app.icon] = operation;
After the download is complete:
Put downloaded images into memory and store them in the sandbox cache
The code stored in the sandbox below can be defined by macro, which can be downloaded from the following demo
if (image) {//Prevent download failure as null assignment causes a crash
Vc.images[app.icon] = image;
Download the completed picture into the sandbox
UIImage--NSData--File (files)
nsdata* ImageData = uiimagepngrepresentation (image);
nsstring* Cachespath = [Nssearchpathfordirectoriesindomains (nscachesdirectory, Nsuserdomainmask, YES) lastObject];
nsstring* FilePath = [Cachespath stringbyappendingpathcomponent:[app.icon lastpathcomponent];
[ImageData Writetofile:filepath Atomically:yes];
}
After performing the above operation, go back to the main thread refresh table,
Remove the download from the Operations dictionary (to prevent operations from getting bigger, and to re-download after the download fails)
Refreshes the picture data for the current row
Self.tableview Reloadrowsatindexpaths:@[indexpath] Withrowanimation:uitableviewrowanimationnone];
Here we do not need [Self.tableview reloadata], because the entire cell will be refreshed, wasting performance.
Of course if you need to do a lot of things in the download operation, you can consider customizing the operation. Since I am just a simple download of the small map there is no custom operation. Note To create an auto-free pool yourself.
Also the ability to clean up the cache is not done
Interested friends can try it on their own
Slag code Download: https://github.com/hongfenglt/CellDownloadImage
At this point the basic implementation of the idea has been completed, this time you will find that actually in the project, the above code does not have any eggs. Because of this operation, Sdwebimgae has been encapsulated, with a code to be done, and this code represents the content than the above operation is more perfect. @@facesymbol@@‖∣°
The above content is only simple optimization, want to further the optimization operation can refer to the following article: http://ios.jobbole.com/82374
iOS dev > Learning-cell asynchronous picture loading optimization, caching mechanism explained