Sdwebimageis a very powerful frame for picture caching. After both asihttp+asyncimage, I have been using afnetworking integrated uiimageview+afnetworking.h, but the latter is actually used for the caching of the pictures Nsurlcache comes with the cache mechanism. and Nsurlcache each time to convert the cached raw data into uiimage, resulting in more data processing and memory operations. The specific comparison is here .
Sdwebimage provides the following three category for caching.
- Mkannotationview (Webcache)
- UIButton (Webcache)
- Uiimageview (Webcache)
Take the most commonly used Uiimageview as an example:
- uiimageview +webcache:
setimagewithurl:placeholderimage:options:
Displays Placeholderimage first, and Sdwebimagemanager finds pictures locally based on the URL.
- sdwebimagemanager :
downloadwithurl:delegate:options:userinfo:
Sdwebimagemanager is the uiimageview+ Webcache linked with Sdimagecache class, sdimagecache: querydiskcacheforkey:delegate:userinfo:
is used to find the picture from the cache based on CacheKey is already in the cache
- If there is already a picture cache in memory, Sdwebimagemanager will callback Sdimagecachedelegate:
imageCache:didFindImage:forKey:userInfo:
- The Uiimageview+webcache callback Sdwebimagemanagerdelegate:
webImageManager:didFinishWithImage:
to display the picture.
- If there is no picture cache in memory, then the build nsinvocationoperation is added to the queue to find whether the picture has been cached from the hard disk.
- Try to read the picture file according to Urlkey in the hard disk cache directory. This step is performed at Nsoperation, so the callback is returned to the main thread for the result
notifyDelegate:
.
- 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, and the image needs to be downloaded and recalled
imageCache:didNotFindImageForKey:userInfo:
.
- Share or regenerate a downloader to
SDWebImageDownloader
start downloading pictures.
- Image download by nsurlconnection to do, to achieve the relevant delegate to determine the picture download, download complete and download failed.
connection:didReceiveData:
In the use of ImageIO done by the picture download progress loading effect.
connectionDidFinishLoading:
After the data download is done, the SDWebImageDecoder
image decoding process is done.
- The image decoding process is done in a 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.
- After the main thread
notifyDelegateOnMainThreadWithInfo:
declares the decoding complete, the imageDecoder:didFinishDecodingImage:userInfo:
callback is given to Sdwebimagedownloader.
imageDownloader:didFinishWithImage:
Callback to Sdwebimagemanager tell the picture download complete.
- Notify all downloaddelegates download complete, callback to show the image where needed.
- Save the picture to Sdimagecache, the memory cache and the hard disk cache are saved at the same time.
- Write files to the hard disk in a separate nsinvocationoperation to avoid slowing down the main thread.
- 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
Images can be downloaded in advance to facilitate subsequent use.
Transfer from http://www.overcode.hk/?p=449
Sdwebimage mechanism for caching pictures (GO)