I. Official pictures

Ii. Process sorting

1. The class that needs to get the image

Call methods to request image resources (APNG, Gif, WebP);

- (void)sd_setImageWithURL:(nullable NSURL *)url 
	  placeholderImage:(nullable UIImage *)placeholder 
      		   options:(SDWebImageOptions)options;
Copy the code

2, UIWebImage + WebCache

Call a method to request an image resource

- (void)sd_internalSetImageWithURL:(nullable NSURL *)url
                  placeholderImage:(nullable UIImage *)placeholder
                           options:(SDWebImageOptions)options
                           context:(nullable SDWebImageContext *)context
                     setImageBlock:(nullable SDSetImageBlock)setImageBlock
                          progress:(nullable SDImageLoaderProgressBlock)progressBlock
                         completed:(nullable SDInternalCompletionBlock)completedBlock;
Copy the code

3, + WebCache UIView

(1) This class manages a dictionary

This dictionary is used to store key-value pairs that follow the SDWebImageOperation protocol. The key is

validOperationKey = NSStringFromClass([self class])
Copy the code

The operation call [operation Cancel] for the key is obtained.

(2) Obtain SDWebImageManager singleton

Get the [SDWebImageManager sharedManager] singleton from the context or directly;

(3) Cancel and store operation

Get the SDWebImageManager singleton object, call the method in 4, SDWebImageManager (1) to get operation, Call [the self sd_setImageLoadOperation: operation forKey: validOperationKey]; Perform [operation cancel] first; Store operation in the dictionary;

4, SDWebImageManager

(1) Obtain operation

Call loadImageWithURL: options: progress: completed: request image resources, This method returns a follow SDWebImageOperation agreement SDWebImageCombinedOperation object operation;

(2) Query key through URL

This class is different from UIView+WebCache (1), which manages the keys retrieved from a dictionary.

NSString *key = [self cacheKeyForURL:url context:context];
Copy the code

(3) Obtain SDImageCachesManager

Obtain [SDImageCachesManager sharedManager] singleton objects from the context or directly. SDImageCachesManager complies with the SDImageCache protocol.

5, SDImageCachesManager

A method is called

- (id<SDWebImageOperation>)queryImageForKey:(NSString*)key options:(SDWebImageOptions)options context:(SDWebImageContext *)context cacheType:(SDImageCacheType)cacheType Completion: SDImageCacheQueryCompletionBlock) completionBlock;Copy the code

QueryOperationPolicy and cacheType fetch operations and caches for different queryOperationPolicy and cacheType

6, SDWebImageManager

Handle the callback from the 5, SDImageCachesManager method call above

CachedImage: cachedImage; cachedImage: cachedImage

Call the method in 5, SDImageCachesManager above to get it in the cache of storage (I understand disk). After getting it, call the method below to store it

// Use the store cache process instead of downloading, and ignore .refreshCached option for now
[self callStoreCacheProcessForOperation:operation url:url options:options context:context downloadedImage:cachedImage downloadedData:cachedData finished:YES progress:progressBlock completed:completedBlock];
Copy the code
(2) cachedImage is not available on disk

(1) A cachedImage has not been obtained in memory, and a cachedImage has not been obtained in disk

- (nullable id<SDWebImageOperation>)requestImageWithURL:(nullable NSURL *)url
                                                options:(SDWebImageOptions)options
                                                context:(nullable SDWebImageContext *)context
                                               progress:(nullable SDImageLoaderProgressBlock)progressBlock
                                              completed:(nullable SDImageLoaderCompletedBlock)completedBlock;
Copy the code

If none is set (that is, the default Settings are used), the following storage logic is followed

CachedImage = cachedImage

// Continue store cache process
[self callStoreCacheProcessForOperation:operation url:url options:options context:context downloadedImage:downloadedImage downloadedData:downloadedData finished:finished progress:progressBlock completed:completedBlock];
Copy the code

(4) Image was obtained

Either cachedImage or downloadImage is acquired, the cachedImage callback returns the image to the upper caller with the following chain: SDImageCachesManager –> 3, UIView+WebCache –> 2, UIImageView+WebCache –> 1, the class that needs to get the image

[self callCompletionBlockForOperation:operation completion:completedBlock image:cachedImage data:cachedData error:nil cacheType:cacheType finished:YES url:url];
Copy the code

Three, notes

  • 1, SDImageCache will register some message notifications during initialization, clear memory image cache when memory warning or back to the background, and clear expired images at the end of the application;
  • 2. After the image is read from the hard disk, the image will be added to the memory cache (if the free memory is too small, the memory cache will be emptied first).
  • 3. Save the image to SDImageCache, memory cache and hard disk cache will be saved at the same time;
  • 4. No matter where the picture is obtained, it will be returned to the picture caller along the return chain for displaying the picture;
  • 5, level 2 cache: memory cache -> disk cache, finally start download;
  • 6. ClearDisk // Clear all images from the disk cache.
  • 7, cleanDisk // Remove expired images from the disk cache;
  • 8. The default time for clearing disk cache is 7 days. The expired cache files are cleared first.
  • 9. MaxCacheSize does not have a default value, so there is no limit on cache space by default.

Other official pictures