Sdwebimage This class library provides a uiimageview category to support the loading of remote pictures from the network. Features such as cache management, asynchronous download, same URL download count control, and optimization.
Sdwebimage supports asynchronous image download + cache, providing Uiimageview+webcacha category for ease of use. Sdwebimage the process of loading pictures:
1. Entry SetImageWithURL:placeholderImage:options: The Placeholderimage is displayed first, and then the Sdwebimagemanager is processed according to the URL.
2. Enter Sdwebimagemanager-downloadwithurl:delegate:options:userinfo:, give Sdimagecache to find the picture from the cache has been downloaded QueryDiskCacheForKey:delegate:userInfo:.
3. First find out if there is a picture from the memory picture cache, if there is already a picture cache in memory, Sdimagecachedelegate callback ImageCache:didFindImage:forKey:userInfo: To Sdwebimagemanager.
4. Sdwebimagemanagerdelegate callback Webimagemanager:didfinishwithimage: to Uiimageview+webcache and other front-end display pictures.
5. If no memory is in the cache, the build nsinvocationoperation is added to the queue to start looking for pictures from the hard disk if it is already cached.
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.
Connection:didreceivedata: The use of ImageIO to download the progress by the picture 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 decoding complete, ImageDecoder:didFinishDecodingImage:userInfo: callback to Sdwebimagedownloader.
Imagedownloader:didfinishwithimage: Callback to Sdwebimagemanager to tell the picture download complete.
16. Notify all downloaddelegates download complete, callback to show the image where needed.
17. Save the picture to Sdimagecache, the memory cache and the hard disk cache are saved simultaneously. Writing files to the hard disk is also done in a separate nsinvocationoperation to avoid slowing down the main thread.
Sdimagecache registers some message notifications at initialization time, cleans up the memory image cache when memory warning or back to the background, and cleans up outdated images when the app is finished.
SDWI also offers uibutton+webcache and mkannotationview+webcache for ease of use.
Sdwebimageprefetcher can pre-download images to facilitate subsequent use.
Where to use the management class:
The most common use of this library is a Category:uiimageview (Webcache) of Uiimageview.
One of the most common ways to do this is to load a picture of the network according to the URL. It is implemented as follows:
-(void) Setimagewithurl: (nsurl *) URL placeholderimage: (uiimage*) placeholder { Sdwebimagemanager *manager = [Sdwebimagemanagersharedmanager]; Remove in progress downloader from queue [Manager Cancelfordelegate:self]; Self.image = placeholder; if (URL) { [Manager Downloadwithurl:url Delegate:self]; } } |
The biggest benefit of this approach is that you can add the network download function directly without changing the UI's class.
Stand-alone asynchronous image download
You may use asynchronous image download alone, you must use downloaderwithurl:delegate: To create a Sdwebimagedownloader instance.
Downloader = [Sdwebimagedownloader downloaderwithurl:url delegate:self];
This method of Sdwebimagedownloaderdelegate protocol imagedownloader:didfinishwithimage: The download will start immediately and complete when it is invoked.
Stand-alone asynchronous image caching
The Sdimagecache class provides an instance of creating an empty cache and uses method Imageforkey: To find the current cache.
UIImage *mycachedimage = [[Sdimagecache Sharedimagecache] imagefromkey:mycachekey];
Storing an image to the cache is a way to use Storeimage:forkey:
[[Sdimagecache Sharedimagecache] Storeimage:myimage Forkey:mycachekey];
By default, images are stored in the memory cache and in the disk cache. If you want to use StoreImage:forKey:toDisk in memory cache only: The third parameter of the method takes a negative value
To replace.
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)
What does Sdimagecache do with 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.
Original link: http://m.blog.csdn.net/blog/Clear__Love/41775251
Sdimage Framework Implementation Principle detailed