Principles of Sdwebimage for iOS framework research and the use of processes
Sdwebimage
This class library provides a Uiimageview category to support loading remote pictures from the network. Features such as cache management, asynchronous download, same URL Download Count control, and optimization.
Sdwebimage support for asynchronous image download + Cache, which provides a Uiimageview+webcacha of the category , easy to use. sdwebimage The process of loading pictures:
- entrance setImageWithURL:placeholderImage:options: will first put Placeholderimage display, and then Sdwebimagemanager according to URL start working with the picture.
- Enter Sdwebimagemanager-downloadwithurl:delegate:options:userinfo: , give Sdimagecache find whether a picture has been downloaded from the cache queryDiskCacheForKey:delegate:userInfo:.
- first from the memory picture cache to find if there is a picture, if there is already a picture cache in memory, sdimagecachedelegate Callback ImageCache:didFindImage:forKey:userInfo: to the Sdwebimagemanager .
- sdwebimagemanagerdelegate Callback Webimagemanager:didfinishwithimage: to the Uiimageview+webcache and other front-end display pictures.
- if not in the memory cache, the build nsinvocationoperation added to the queue to start looking for pictures from the hard disk is already cached.
- according to Urlkey try to read the picture file in the hard disk cache directory. This step is performed at nsoperation , so the callback notifydelegate is returned to the main thread :.
- 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.
- If the picture is not read from the hard disk cache directory, the picture is not present in all caches, the picture needs to be downloaded, the callback ImageCache:didNotFindImageForKey:userInfo: .
- share or regenerate a downloader Sdwebimagedownloader start downloading pictures.
- image Download by nsurlconnection to do, to achieve the relevant Delegate to determine the image download, download complete and download failed.
- Connection:didreceivedata: used in ImageIO done by the picture download progress loading effect.
- connectiondidfinishloading: after the data download is complete, give Sdwebimagedecoder do picture decoding processing.
- picture decoding processing in a Nsoperationqueue complete without slowing 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.
- in the main thread Notifydelegateonmainthreadwithinfo: The decoding is complete, ImageDecoder:didFinishDecodingImage:userInfo: Callback to Sdwebimagedownloader .
- Imagedownloader:didfinishwithimage: Callback to Sdwebimagemanager let the picture download complete.
- notify all the downloaddelegates The download is complete and the callback shows the image to the desired place.
- 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 Some message notifications are registered at the time of initialization, clearing the memory image cache when the memory is warning or retiring to the background, and cleaning out outdated images at the end of the application.
- Sdwi also provides a Uibutton+webcache and the Mkannotationview+webcache , easy to use.
- Sdwebimageprefetcher images can be downloaded in advance to facilitate subsequent use.
Where to use the management class:
The most common use of this library is Uiimageview of a Category : Uiimageview (Webcache) .
one of the most commonly used methods in this area is to URL , load a picture of the network. 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;
off (URL)
{
[manager downloadwithurl: url delegate: Self ];
}
}
|
The best thing about this approach is that you don't need to change UI class, add the network download function directly.
Stand-alone asynchronous image download
Asynchronous picture downloads may be used alone, you must use downloaderwithurl:delegate: to create a Sdwebimagedownloader instance.
Downloader =[sdwebimagedownloader Downloaderwithurl:url delegate:self];
this sdwebimagedownloaderdelegate method of the Protocol Imagedownloader:didfinishwithimage: When it is called, the download starts and finishes immediately.
Stand-alone asynchronous image caching
Sdimagecache class provides an instance of creating an empty cache and using methods Imageforkey: to find the current cache.
Uiimage*mycachedimage = [[Sdimagecache Sharedimagecache] imagefromkey:mycachekey];
storing an image to a cache is a way to use Storeimage:forkey:
[[Sdimagecachesharedimagecache] 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:
through the Uiimageview class extension to implement the task of asynchronously loading a replacement picture.
the main objects to use:
1,Uiimageview (Webcache)class, Ingress encapsulation, implementing callbacks after reading a picture complete
2,Sdwebimagemanager, the station that manages the picture, records those pictures being read.
Read to downlevelCache(CallSdimagecache), or read the object to the network (callingSdwebimagedownloader).
ImplementSdimagecacheand theSdwebimagedownloaderthe callback.
3,Sdimagecache, according toURLof theMD5Abstract Store and read images (implementations exist in memory or on hard disk implementations)
implement picture and memory cleanup work.
4,Sdwebimagedownloader, according toURLread data to the network (two ways of enabling partial and full read and then notifying callbacks)
Sdimagecache How to Do data management ?
Sdimagecache divided into two parts, one is the memory level, one is the hard disk level.
the memory plane is quite a buffer to Key-value stored in the form of a picture. 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 resource, first search the memory level of 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 Decoder , put the picture object on the memory plane for backup, and then return to the call layer.
Ios-sdwebimage principle and process of use