Principles and usage of SDWebImage and principles of sdwebimage

Source: Internet
Author: User
Tags md5 digest

Principles and usage of SDWebImage and principles of sdwebimage

This class library provides a UIImageView category to support loading remote images from the network. It features Cache Management, asynchronous download, control of the number of downloads from the same URL, and optimization.

 

How to load images using SDWebImage

  1. The entry setImageWithURL: placeholderImage: options: displays the placeholderImage, and SDWebImageManager processes the Image Based on the URL.
  2. Go to SDWebImageManager-downloadWithURL: delegate: options: userInfo:, and submit it to SDImageCache to check whether the image has been downloaded from the cache. queryDiskCacheForKey: delegate: userInfo :.
  3. First, check whether there are images in the memory image cache. If there is already an image cache in the memory, SDImageCacheDelegate calls back imageCache: didFindImage: forKey: userInfo: To SDWebImageManager.
  4. SDWebImageManagerDelegate callback webImageManager: didFinishWithImage: displays images at the front end of UIImageView + WebCache.
  5. If the memory cache does not exist, generate NSInvocationOperation and add it to the queue. Start from the hard disk to find whether the image has been cached.
  6. Try to read image files in the hard disk cache directory based on the URLKey. This step is performed in NSOperation. Therefore, the main thread returns the result callback policydelegate :.
  7. If the image is read from the hard disk in the previous operation, add the image to the memory cache (if the idle memory is too small, the memory cache will be cleared first ). SDImageCacheDelegate callback imageCache: didFindImage: forKey: userInfo :. Then, the callback shows the image.
  8. If the image cannot be read from the hard disk cache directory, it indicates that no image exists in the cache. You need to download the image and call back imageCache: didNotFindImageForKey: userInfo :.
  9. Share or regenerate a download tool SDWebImageDownloader to download images.
  10. The NSURLConnection method is used to download images. The delegate method is used to determine whether the images are being downloaded, completed, and failed to be downloaded.
  11. Connection: didReceiveData: The ImageIO is used to load data according to the image download progress.
  12. ConnectionDidFinishLoading: After the data is downloaded, it is sent to SDWebImageDecoder for image decoding.
  13. The image decoding process is completed in an NSOperationQueue, and the main thread UI is not slowed down. If you need to process the downloaded image twice, it is better to do so here, which will be much more efficient.
  14. In the main thread yydelegateonmainthreadwithinfo: the decoding is completed, and imageDecoder: didFinishDecodingImage: userInfo: calls back to SDWebImageDownloader.
  15. ImageDownloader: didFinishWithImage: calls back to SDWebImageManager to notify the image download is complete.
  16. This notification notifies all downloadDelegates that the download is complete and calls back to display the image to the desired location.
  17. Save the image to SDImageCache, and save both the memory cache and the hard disk cache. Writing files to the hard disk is also completed in separate NSInvocationOperation to Avoid dragging the main thread slowly.
  18. SDImageCache registers some message notifications during initialization. It cleans up the memory image cache when the memory is warned or returned to the background, and clears expired images when the application ends.
  19. SDWI also provides UIButton + WebCache and MKAnnotationView + WebCache for ease of use.
  20. SDWebImagePrefetcher can download images in advance for later use.

 

Function of SDWebImage Library

You can use the class extension of UIImageView to asynchronously load and replace images.

Main objects used:

1. UIImageView (WebCache) category, entry encapsulation, callback after reading the image

2. SDWebImageManager, a transfer station for image management, records the images being read.

Read the Cache from the lower layer (call SDImageCache) or read the object from the Network (call SDWebImageDownloader ).

Implement callback for SdImageCache and SDWebImageDownloader

3. SDImageCache: stores and reads images based on the URL's MD5 Digest (implemented in memory or on the keyboard)

Image and memory cleanup

4. SDWebImageDownlader: reads data from the network based on the URL (two methods are available: partial reading and full reading before notification callback)

Other types:

SDWebImageDecoder: Decompress the image once Asynchronously

 

Q:

How does SDImageCache manage data?

SDImageCache consists of two parts: memory and hard disk.

The memory layer is equivalent to a cache, which stores images in the form of Key-Value. When the memory is insufficient, All cached images are cleared.

Use the search file system method for Management. The file replacement method is in the unit of time.

When SDWebImageManager asks SDImageCache for resources, it first searches for data at the memory level. If any data is directly returned, it will access the disk, read the image from the disk, and then perform Decoder, put the image object in the memory layer for backup, and then return to the call layer.

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.