Sdwebimge principle
One, the first superscript answer
1) uiimageview+webcache:setimagewithurl:placeholderimage:options: Show Placeholderimage First, and Sdwebimagemanager by URL To find a picture locally.
2) SDWebImageManager:downloadWithURL:delegate:options:userInfo:SDWebImageManager is the uiimageview+ Webcache classes linked with Sdimagecache, SDImageCache:queryDiskCacheForKey:delegate:userInfo: Used to find images from the cache based on CacheKey is already in the cache
3) If there is already a picture cache in memory, Sdwebimagemanager will callback SDImageCacheDelegate:imageCache:didFindImage:forKey:userInfo:
4) and Uiimageview+webcache callback SDWebImageManagerDelegate:webImageManager:didFinishWithImage: To display the picture.
5) If there is no picture cache in memory, then the build nsinvocationoperation is added to the queue, from the hard disk to find whether the picture has been downloaded cache.
6) Try to read the picture file according to the Urlkey in the hard disk cache directory. This step is performed at Nsoperation, so the callback notifydelegate is returned to the main thread:.
7) If the previous action reads a picture from the hard disk, the picture is added to the in-memory cache (if the free memory is too small, the memory cache is emptied first). Sdimagecachedelegate callback ImageCache:didFindImage:forKey:userInfo:. And then callback the display image.
8) If the picture is not read from the hard disk cache directory, the picture is not present in all caches, the image needs to be downloaded, callback ImageCache:didNotFindImageForKey:userInfo:.
9) share or regenerate a downloader sdwebimagedownloader start downloading pictures.
10) Image download by nsurlconnection to do, to achieve the relevant delegate to determine the picture download, download complete and download failed.
One) Connection:didreceivedata: In the use of ImageIO made by the picture download progress loading effect.
Connectiondidfinishloading: After the data download is finished, give sdwebimagedecoder to do the image decoding processing.
13) Image decoding processing is done in one nsoperationqueue and does not slow down the main thread UI. If there is a need to download the image two times, it is best to complete here, the efficiency will be much better.
14) in the main thread Notifydelegateonmainthreadwithinfo: announce the completion of decoding, ImageDecoder:didFinishDecodingImage:userInfo: callback to Sdwebimagedownloader.
Imagedownloader:didfinishwithimage: Callback to Sdwebimagemanager to inform the picture download completed.
16) Notify all downloaddelegates download complete, callback to the desired place to display pictures.
17) Save the picture to Sdimagecache, the memory cache and the hard disk cache are saved simultaneously.
18) Write the file to the hard disk in a separate nsinvocationoperation to avoid slowing down the main thread.
19) If you are running on iOS, Sdimagecache will register notification to uiapplicationdidreceivememorywarningnotification when initializing and Uiapplicationwillterminatenotification, in memory warning, clean up the memory image cache, the end of the application to clean up the expired image.
Sdwebimageprefetcher can pre-download images for easy follow-up use.
Second, the role of the Sdwebimage library
The task of asynchronously loading a replacement picture is achieved by extending the category of Uiimageview.
The main objects to use:
1, Uiimageview (webcache) category, ingress encapsulation, implementation of reading the image after the completion of the callback
2, Sdwebimagemanager, the management of the picture Transfer station, record those pictures are reading.
Read the cache to the downlevel (call Sdimagecache), or read the object to the network (call Sdwebimagedownloader).
Implement callbacks for Sdimagecache and Sdwebimagedownloader.
3, Sdimagecache, according to the URL of the MD5 digest to store and read the picture (Implementation exists in memory or there are two implementations of the hard disk)
Implement picture and memory cleanup work.
4, Sdwebimagedownloader, according to the URL to read data to the network (to achieve partial read and all read and then notify callback two ways)
Other classes:
Sdwebimagedecoder, an asynchronous image was decompressed??
Three, the answer analysis
1, Sdimagecache is how to do data management?
Sdimagecache is divided into two parts, one is the memory level, the other is the hard disk level.
The memory plane is quite a buffer, storing the picture in the form of Key-value. All cached images are cleared when there is not enough memory.
In the way of searching the file system to do management, the file replacement method is in time, the rejection time is more than a week of picture files.
When Sdwebimagemanager to Sdimagecache to the resources, first search the memory plane of the data, if there is a direct return, there is no way to access the disk, the picture is read from the disk, and then do the decoder, put the picture object to the memory plane to do the backup, and then return to the call layer.
2, why must do decoder?
Now I understand that since UIImage's Imagewithdata function is to extract data into a ARGB image every time a drawing is made,
So in each drawing, there will be a decompression operation, so inefficient, but only the instantaneous memory requirements.
In order to improve the efficiency through the sdwebimagedecoder will be wrapped in data under the resource decompression, and then draw on another picture, so that the new picture will no longer need to re-extract. This practice is typical of space-time-changing practices.
iOS Development engineer Face question (i)