SDWebImageManager
SDWebImageManager
是一个管理图片缓存和下载逻辑的一个管理类,将SDImageCache
和SDWebImageDownloader
结合起来处理图片的缓存和下载逻辑。
我们先来看下类的定义SDWebImageManager.h
文件,代码如下:
@interface SDWebImageManager : NSObject
@property (weak, nonatomic, nullable) id <SDWebImageManagerDelegate> delegate;
@property (strong, nonatomic, readonly, nullable) SDImageCache *imageCache;
@property (strong, nonatomic, readonly, nullable) SDWebImageDownloader *imageDownloader;
// 返回cacheKey
@property (nonatomic, copy, nullable) SDWebImageCacheKeyFilterBlock cacheKeyFilter;
/**
*
缓存序列化程序是一个块,用于将解码后的图像、源下载的数据转换为用于存储到磁盘缓存的实际数据。如果返回nil,则表示要从图像实例生成数据,请参见“SDImageCache”。
*/
// cacheData缓存数据序列化处理
@property (nonatomic, copy, nullable) SDWebImageCacheSerializerBlock cacheSerializer;
/**
* Returns global SDWebImageManager instance.
*
* @return SDWebImageManager shared instance
*/
+ (nonnull instancetype)sharedManager;
/**
* Allows to specify instance of cache and image downloader used with image manager.
自定义cache和downloader
* @return new instance of `SDWebImageManager` with specified cache and downloader.
*/
- (nonnull instancetype)initWithCache:(nonnull SDImageCache *)cache downloader:(nonnull SDWebImageDownloader *)downloader NS_DESIGNATED_INITIALIZER;
/**
加载图片
@param url 图片链接
@param options SDWebImageOptions图片加载选项
@param progressBlock 图片下载进度回调
@param completedBlock 图片下载完成回调
@return 具体加载图片的操作 SDWebImageOperation
*/
- (nullable id <SDWebImageOperation>)loadImageWithURL:(nullable NSURL *)url
options:(SDWebImageOptions)options
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
completed:(nullable SDInternalCompletionBlock)completedBlock;
/**
* Saves image to cache for given URL
* 根据url缓存图片
* @param image The image to cache 要缓存的图片
* @param url The URL to the image 图片链接
*
*/
- (void)saveImageToCache:(nullable UIImage *)image forURL:(nullable NSURL *)url;
/**
* Cancel all current operations
取消所有正在下载的操作
*/
- (void)cancelAll;
/**
* Check one or more operations running
确定是否有图片正在下载
*/
- (BOOL)isRunning;
/**
* Async check if image has already been cached
异步检查图片是否已经被缓存
*
* @param url image url 图片链接
* @param completionBlock the block to be executed when the check is finished
* 检查完成回调
* @note the completion block is always executed on the main queue
*/
- (void)cachedImageExistsForURL:(nullable NSURL *)url
completion:(nullable SDWebImageCheckCacheCompletionBlock)completionBlock;
/**
* Async check if image has already been cached on disk only
异步检查图片是否仅缓存在磁盘里
*
* @param url image url 图片链接
* @param completionBlock the block to be executed when the check is finished
* 检查完成回调
* @note the completion block is always executed on the main queue
*/
- (void)diskImageExistsForURL:(nullable NSURL *)url
completion:(nullable SDWebImageCheckCacheCompletionBlock)completionBlock;
/**
*Return the cache key for a given URL
根据url查询缓存的key
*/
- (nullable NSString *)cacheKeyForURL:(nullable NSURL *)url;
@end
从上面图片类的定义中可以看出SDWebImageManager
是一个单例类,同时也支持自定义SDImageCache
和SDWebImageDownloader
。如下:
/**
* Returns global SDWebImageManager instance.
*
* @return SDWebImageManager shared instance
*/
+ (nonnull instancetype)sharedManager;
/**
* Allows to specify instance of cache and image downloader used with image manager.
自定义cache和downloader
* @return new instance of `SDWebImageManager` with specified cache and downloader.
*/
- (nonnull instancetype)initWithCache:(nonnull SDImageCache *)cache downloader:(nonnull SDWebImageDownloader *)downloader NS_DESIGNATED_INITIALIZER;
同时也支持实现SDWebImageManagerDelegate
的代理,支持一些自定义的设置,是否支持从网络下载图片、自定义失败url以及自定义图片转换的规则,代码如下:
@property (weak, nonatomic, nullable) id <SDWebImageManagerDelegate> delegate;
@protocol SDWebImageManagerDelegate <NSObject>
@optional
/**
判定某一个图片在缓存中没找到时,是否从网络下载
@param imageManager imageManager
@param imageURL 图片链接
@return 是否从网络下载,默认为Yes
*/
- (BOOL)imageManager:(nonnull SDWebImageManager *)imageManager shouldDownloadImageForURL:(nullable NSURL *)imageURL;
/**
* Controls the complicated logic to mark as failed URLs when download error occur.
控制在发生下载错误时标记为失败URL的复杂逻辑。
* If the delegate implement this method, we will not use the built-in way to mark URL as failed based on error code;
如果委托实现了这个方法,我们将不会使用内置的方法根据错误代码将URL标记为失败;
@param imageManager The current `SDWebImageManager`
@param imageURL The url of the image
@param error The download error for the url
@return Whether to block this url or not. Return YES to mark this URL as failed.
*/
- (BOOL)imageManager:(nonnull SDWebImageManager *)imageManager shouldBlockFailedURL:(nonnull NSURL *)imageURL withError:(nonnull NSError *)error;
/**
* Allows to transform the image immediately after it has been downloaded and just before to cache it on disk and memory.
在下载后,图片缓存到磁盘和内存之前转换图片
* NOTE: This method is called from a global queue in order to not to block the main thread.
为了不阻塞主线程,这个方法是在全局队列中调用的
*
* @param imageManager The current `SDWebImageManager`
* @param image The image to transform 要转换的图片
* @param imageURL The url of the image to transform
*
* @return The transformed image object. 转换好的图片
*/
- (nullable UIImage *)imageManager:(nonnull SDWebImageManager *)imageManager transformDownloadedImage:(nullable UIImage *)image withURL:(nullable NSURL *)imageURL;
@end
同时也支持,自定义cacheKey缓存key的生成规则以及自定义cacheData的序列化处理.代码如下
// 返回cacheKey
@property (nonatomic, copy, nullable) SDWebImageCacheKeyFilterBlock cacheKeyFilter;
/**
*
缓存序列化程序是一个块,用于将解码后的图像、源下载的数据转换为用于存储到磁盘缓存的实际数据。如果返回nil,则表示要从图像实例生成数据,请参见“SDImageCache”。
*/
// cacheData缓存数据序列化处理
@property (nonatomic, copy, nullable) SDWebImageCacheSerializerBlock cacheSerializer;
网友评论