Sdwebimage Picture caching mechanism

Source: Internet
Author: User

What is the advantage of sdwebimage compared to the cache that comes with the iOS system?

Starting with iOS5, IOS Nsurlcache does memory and disk caching for native HTTP response. Every time the cache hits (the cache hits the memory or the disk has the data to be requested), the app must turn the native data into UIImage. This requires a lot of operations, such as data analysis conversions, memory copies, and so on.

On the other hand, Sdwebimage caches the uiimage in memory, and the original compressed (but decoded) image file is stored on the disk. UIImage is in memory with Nscache, so there is no need to copy the operation, and as long as the app needs memory, memory can be freed.

In addition, the first time you use UIImage in a uiimageview, the picture decompression is usually in the main thread, while in the Sdwebimagedecoder forced decompression error is done in the background (not the main thread).

The Sdwebimage provides three category for caching:

    • Mkannotationview (Webcache)
    • UIButton (Webcache)
    • Uiimageview (Webcache)

Sdwebimage caching mechanism (take uiimageview as an example):

  1. Uiimageview+webcache: setImageWithURL:placeholderImage:options: The placeholderimage is displayed first, and the image is found locally by sdwebimagemanager based on the URL.
  2. Sdwebimagemanager: downloadWithURL:delegate:options:userInfo:  Sdwebimagemanager is the class that links Uiimageview+webcache with Sdimagecache, Sdimagecache: queryDiskCacheForKey:delegate:userInfo: Used to find whether a picture is already in the cache from the cache based on CacheKey
  3. If there is already a picture cache in memory, Sdwebimagemanager will callback Sdimagecachedelegate:imageCache:didFindImage:forKey:userInfo:
  4. The 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 to find whether the picture has been cached from the hard disk.
  6. Try to read the picture file according to Urlkey in the hard disk cache directory. This step is performed at Nsoperation, so the callback notifyDelegate: is returned to the main thread for the result.
  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, and the imageCache:didNotFindImageForKey:userInfo: image needs to be downloaded and recalled.
  9. Share or regenerate a downloader SDWebImageDownloader to 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.
  11. connection:didReceiveData:In the use of ImageIO done by the picture download progress loading effect.
  12. connectionDidFinishLoading: after the data download is done, the image decoding process is done. SDWebImageDecoder
  13. 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.
  14. After the main notifyDelegateOnMainThreadWithInfo: thread declares the decoding imageDecoder:didFinishDecodingImage:userInfo: complete, the callback is given to Sdwebimagedownloader.
  15. imageDownloader:didFinishWithImage:Callback to Sdwebimagemanager 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 at the same time.
  18. Write files 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.
  20. SDWebImagePrefetcherImages can be downloaded in advance to facilitate subsequent use.

Transferred from: http://www.overcode.hk/?p=449

Sdwebimage Picture caching mechanism

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.