SDWebImage 4.X source code analysis

Posted by TechMistress on Sat, 16 Nov 2019 19:57:39 +0100

SDWebImage UML class diagram

SDWebImage flowchart

1.UIImageView+WebCache

Interface separation principle: provide specific interfaces for specific functions. Instead of using a single total interface including all functions, these interfaces should be divided according to functions to reduce dependency. Users cannot be forced to rely on interfaces they do not use.

- (void)sd_setImageWithURL:(nullable NSURL *)url
                 completed:(nullable SDExternalCompletionBlock)completedBlock;

- (void)sd_setImageWithURL:(nullable NSURL *)url
          placeholderImage:(nullable UIImage *)placeholder
                   options:(SDWebImageOptions)options
                 completed:(nullable SDExternalCompletionBlock)completedBlock;

- (void)sd_setImageWithURL:(nullable NSURL *)url
          placeholderImage:(nullable UIImage *)placeholder
                   options:(SDWebImageOptions)options
                  progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
                 completed:(nullable SDExternalCompletionBlock)completedBlock;

2.UIView+WebCache

- (void)sd_internalSetImageWithURL:(nullable NSURL *)url
                  placeholderImage:(nullable UIImage *)placeholder
                           options:(SDWebImageOptions)options
                      operationKey:(nullable NSString *)operationKey
                     setImageBlock:(nullable SDSetImageBlock)setImageBlock
                          progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
                         completed:(nullable SDExternalCompletionBlock)completedBlock
                           context:(nullable NSDictionary<NSString *, id> *)context
{
1\. Cancel the loading task currently in progress operation
2\. take url Save as attribute set sd_imageURL
3\. Set occupation map 
4\. If URL Existing, by url Load picture if url by nil,Direct callback completedBlock
5\. Set the loaded picture and call back completedBlock 
}

3.SDWebImageManager

enumeration
//Options for picture acquisition and settings
typedef NS_OPTIONS(NSUInteger, SDWebImageOptions) {
    SDWebImageRetryFailed = 1 << 0,
    SDWebImageLowPriority = 1 << 1,
    SDWebImageCacheMemoryOnly = 1 << 2,
    SDWebImageProgressiveDownload = 1 << 3,
    SDWebImageRefreshCached = 1 << 4,
    SDWebImageContinueInBackground = 1 << 5,
    SDWebImageHandleCookies = 1 << 6,
    SDWebImageAllowInvalidSSLCertificates = 1 << 7,
    SDWebImageHighPriority = 1 << 8,
    SDWebImageDelayPlaceholder = 1 << 9,
    SDWebImageTransformAnimatedImage = 1 << 10,
    SDWebImageAvoidAutoSetImage = 1 << 11,
    SDWebImageScaleDownLargeImages = 1 << 12,
    SDWebImageQueryDataWhenInMemory = 1 << 13,
    SDWebImageQueryDiskSync = 1 << 14,
    SDWebImageFromCacheOnly = 1 << 15,
    SDWebImageForceTransition = 1 << 16
};

Properties in. h file

@property (weak, nonatomic, nullable) id <SDWebImageManagerDelegate> delegate;
@property (strong, nonatomic, readonly, nullable) SDImageCache *imageCache;
@property (strong, nonatomic, readonly, nullable) SDWebImageDownloader *imageDownloader;
@property (nonatomic, copy, nullable) SDWebImageCacheKeyFilterBlock cacheKeyFilter;

Method in. h file

+ (nonnull instancetype)sharedManager;

//Loading pictures
- (nullable id <SDWebImageOperation>)loadImageWithURL:(nullable NSURL *)url
                                              options:(SDWebImageOptions)options
                                             progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
                                            completed:(nullable SDInternalCompletionBlock)completedBlock;

//Save picture to cache
- (void)saveImageToCache:(nullable UIImage *)image forURL:(nullable NSURL *)url;

//Cancel current operations
- (void)cancelAll;
//Check if operations is in progress
- (BOOL)isRunning;

//Check whether the picture exists in the cache
- (void)cachedImageExistsForURL:(nullable NSURL *)url
                     completion:(nullable SDWebImageCheckCacheCompletionBlock)completionBlock;
- (void)diskImageExistsForURL:(nullable NSURL *)url
                   completion:(nullable SDWebImageCheckCacheCompletionBlock)completionBlock;

//Return the cached key according to the url
 (nullable NSString *)cacheKeyForURL:(nullable NSURL *)url;

Properties in. m file

@property (strong, nonatomic, readwrite, nonnull) SDImageCache *imageCache;
@property (strong, nonatomic, readwrite, nonnull) SDWebImageDownloader *imageDownloader;
//Failed url array
@property (strong, nonatomic, nonnull) NSMutableSet<NSURL *> *failedURLs;
//Array of currently executed tasks
@property (strong, nonatomic, nonnull) NSMutableArray<SDWebImageCombinedOperation *> *runningOperations;

Method in. m file

//Initialization
+ (nonnull instancetype)sharedManager;
- (nonnull instancetype)init
- (nonnull instancetype)initWithCache:(nonnull SDImageCache *)cache downloader:(nonnull SDWebImageDownloader *)downloader

- (nullable NSString *)cacheKeyForURL:(nullable NSURL *)url

//Zoom picture
- (nullable UIImage *)scaledImageForKey:(nullable NSString *)key image:(nullable UIImage *)image

//Find pictures
- (void)cachedImageExistsForURL:(nullable NSURL *)url
                     completion:(nullable SDWebImageCheckCacheCompletionBlock)completionBlock
- (void)diskImageExistsForURL:(nullable NSURL *)url
                   completion:(nullable SDWebImageCheckCacheCompletionBlock)completionBlock

//Loading pictures
- (id <SDWebImageOperation>)loadImageWithURL:(nullable NSURL *)url
                                     options:(SDWebImageOptions)options
                                    progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
                                   completed:(nullable SDInternalCompletionBlock)completedBlock

//Cache images
- (void)saveImageToCache:(nullable UIImage *)image forURL:(nullable NSURL *)url

//operation related
- (void)cancelAll
- (BOOL)isRunning

//Remove operation from running operations
- (void)safelyRemoveOperationFromRunning:(nullable SDWebImageCombinedOperation*)operation

//Main thread callback
- (void)callCompletionBlockForOperation:(nullable SDWebImageCombinedOperation*)operation
                             completion:(nullable SDInternalCompletionBlock)completionBlock
                                  error:(nullable NSError *)error
                                    url:(nullable NSURL *)url 
- (void)callCompletionBlockForOperation:(nullable SDWebImageCombinedOperation*)operation
                             completion:(nullable SDInternalCompletionBlock)completionBlock
                                  image:(nullable UIImage *)image
                                   data:(nullable NSData *)data
                                  error:(nullable NSError *)error
                              cacheType:(SDImageCacheType)cacheType
                               finished:(BOOL)finished
                                    url:(nullable NSURL *)url

The core task of SDWebImageManager is implemented by loadImageWithURL method:

- (id <SDWebImageOperation>)loadImageWithURL:(nullable NSURL *)url
                                     options:(SDWebImageOptions)options
                                    progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
                                   completed:(nullable SDInternalCompletionBlock)completedBlock

{
1.Error checking
2.Establish SDWebImageCombinedOperation object
3.Determine whether it has ever failed to download url
4.If this url The length of is 0, or the download has failed and is not set SDWebImageRetryFailed,Direct callback completedBlock,And return directly
5.Add to operation reach runningOperations in
6.Read cache
6.1 Return if there is a cache operation
6.2 If not, download the image, cache it, and return operation
}

Topics: Attribute