该文章阅读的SDWebImage的版本为4.3.3。
这两个分类都是UIImageView
的分类,功能也都是设置图片,所以就写在一起。
1.UIImageView+WebCache
1.1.公共方法
1.1.1.设置静态图像
/**
设置指定图像链接
*/
- (void)sd_setImageWithURL:(nullable NSURL *)url NS_REFINED_FOR_SWIFT;
复制代码
/**
设置指定图像链接和占位图
*/
- (void)sd_setImageWithURL:(nullable NSURL *)url
placeholderImage:(nullable UIImage *)placeholder NS_REFINED_FOR_SWIFT;
复制代码
/**
设置指定图像链接、占位图和可选项
*/
- (void)sd_setImageWithURL:(nullable NSURL *)url
placeholderImage:(nullable UIImage *)placeholder
options:(SDWebImageOptions)options NS_REFINED_FOR_SWIFT;
复制代码
/**
设置指定图像链接和完成回调
*/
- (void)sd_setImageWithURL:(nullable NSURL *)url
completed:(nullable SDExternalCompletionBlock)completedBlock;
复制代码
/**
设置指定图像链接、占位图和完成回调
*/
- (void)sd_setImageWithURL:(nullable NSURL *)url
placeholderImage:(nullable UIImage *)placeholder
completed:(nullable SDExternalCompletionBlock)completedBlock NS_REFINED_FOR_SWIFT;
复制代码
/**
设置指定图像链接、占位图、可选项和完成回调
*/
- (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;
复制代码
1.1.2.设置动态图像
/**
设置指定动图的图像链接数组
*/
- (void)sd_setAnimationImagesWithURLs:(nonnull NSArray<NSURL *> *)arrayOfURLs;
复制代码
/**
取消当前动图图像加载
*/
- (void)sd_cancelCurrentAnimationImagesLoad;
复制代码
1.2.私有方法
/**
获取保存动图加载操作集合对象
*/
- (NSPointerArray *)sd_animationOperationArray {
@synchronized(self) {
NSPointerArray *operationsArray = objc_getAssociatedObject(self, &animationLoadOperationKey);
if (operationsArray) {
return operationsArray;
}
operationsArray = [NSPointerArray weakObjectsPointerArray];
objc_setAssociatedObject(self, &animationLoadOperationKey, operationsArray, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
return operationsArray;
}
}
复制代码
1.3.实现
1.3.1.设置静态图像方法实现
- (void)sd_setImageWithURL:(nullable NSURL *)url {
// 调用全能方法
[self sd_setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:nil];
}
复制代码
- (void)sd_setImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder {
// 调用全能方法
[self sd_setImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:nil];
}
复制代码
- (void)sd_setImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options {
// 调用全能方法
[self sd_setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:nil];
}
- (void)sd_setImageWithURL:(nullable NSURL *)url completed:(nullable SDExternalCompletionBlock)completedBlock {
// 调用全能方法
[self sd_setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:completedBlock];
}
复制代码
- (void)sd_setImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder completed:(nullable SDExternalCompletionBlock)completedBlock {
// 调用全能方法
[self sd_setImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:completedBlock];
}
复制代码
- (void)sd_setImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options completed:(nullable SDExternalCompletionBlock)completedBlock {
// 调用全能方法
[self sd_setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:completedBlock];
}
复制代码
- (void)sd_setImageWithURL:(nullable NSURL *)url
placeholderImage:(nullable UIImage *)placeholder
options:(SDWebImageOptions)options
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
completed:(nullable SDExternalCompletionBlock)completedBlock {
// 调用UIView+WebCache分类方法加载图像
[self sd_internalSetImageWithURL:url
placeholderImage:placeholder
options:options
operationKey:nil
setImageBlock:nil
progress:progressBlock
completed:completedBlock];
}
复制代码
1.3.2.设置动态图像方法实现
- (void)sd_setAnimationImagesWithURLs:(nonnull NSArray<NSURL *> *)arrayOfURLs {
// 取消当前动图加载
[self sd_cancelCurrentAnimationImagesLoad];
// 获取保存图片加载操作的集合对象
NSPointerArray *operationsArray = [self sd_animationOperationArray];
// 遍历图像数组
[arrayOfURLs enumerateObjectsUsingBlock:^(NSURL *logoImageURL, NSUInteger idx, BOOL * _Nonnull stop) {
__weak __typeof(self) wself = self;
// 加载图像
id <SDWebImageOperation> operation = [[SDWebImageManager sharedManager] loadImageWithURL:logoImageURL options:0 progress:nil completed:^(UIImage *image, NSData *data, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
__strong typeof(wself) sself = wself;
if (!sself) return;
// 主线程异步回调
dispatch_main_async_safe(^{
// 停止动画
[sself stopAnimating];
if (sself && image) {
// 获取当前图像数组
NSMutableArray<UIImage *> *currentImages = [[sself animationImages] mutableCopy];
// 如果没有就创建一个可变数组保存图像
if (!currentImages) {
currentImages = [[NSMutableArray alloc] init];
}
// 将图像添加到可变数组中
while ([currentImages count] < idx) {
[currentImages addObject:image];
}
// 保存当前图像
currentImages[idx] = image;
// 设置图像数组
sself.animationImages = currentImages;
[sself setNeedsLayout];
}
// 开始动画
[sself startAnimating];
});
}];
// 保存加载操作
@synchronized (self) {
[operationsArray addPointer:(__bridge void *)(operation)];
}
}];
}
复制代码
- (void)sd_cancelCurrentAnimationImagesLoad {
// 获取保存图像操作的集合对象
NSPointerArray *operationsArray = [self sd_animationOperationArray];
// 遍历并取消操作
if (operationsArray) {
@synchronized (self) {
for (id operation in operationsArray) {
if ([operation conformsToProtocol:@protocol(SDWebImageOperation)]) {
[operation cancel];
}
}
operationsArray.count = 0;
}
}
}
复制代码
2.UIImageView+HighlightedWebCache
2.1.公共方法
/**
设置指定高亮图像链接
*/
- (void)sd_setHighlightedImageWithURL:(nullable NSURL *)url NS_REFINED_FOR_SWIFT;
复制代码
/**
设置指定高亮图像链接和可选项
*/
- (void)sd_setHighlightedImageWithURL:(nullable NSURL *)url
options:(SDWebImageOptions)options NS_REFINED_FOR_SWIFT;
复制代码
/**
设置指定高亮图像链接和完成回调
*/
- (void)sd_setHighlightedImageWithURL:(nullable NSURL *)url
completed:(nullable SDExternalCompletionBlock)completedBlock NS_REFINED_FOR_SWIFT;
复制代码
/**
设置指定高亮图像链接、可选项和完成回调
*/
- (void)sd_setHighlightedImageWithURL:(nullable NSURL *)url
options:(SDWebImageOptions)options
completed:(nullable SDExternalCompletionBlock)completedBlock;
复制代码
/**
设置指定高亮图像链接、可选项、进度监听和完成回调
*/
- (void)sd_setHighlightedImageWithURL:(nullable NSURL *)url
options:(SDWebImageOptions)options
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
completed:(nullable SDExternalCompletionBlock)completedBlock;
复制代码
2.2.实现
- (void)sd_setHighlightedImageWithURL:(nullable NSURL *)url {
// 调用全能方法
[self sd_setHighlightedImageWithURL:url options:0 progress:nil completed:nil];
}
复制代码
- (void)sd_setHighlightedImageWithURL:(nullable NSURL *)url options:(SDWebImageOptions)options {
// 调用全能方法
[self sd_setHighlightedImageWithURL:url options:options progress:nil completed:nil];
}
复制代码
- (void)sd_setHighlightedImageWithURL:(nullable NSURL *)url completed:(nullable SDExternalCompletionBlock)completedBlock {
// 调用全能方法
[self sd_setHighlightedImageWithURL:url options:0 progress:nil completed:completedBlock];
}
复制代码
- (void)sd_setHighlightedImageWithURL:(nullable NSURL *)url options:(SDWebImageOptions)options completed:(nullable SDExternalCompletionBlock)completedBlock {
// 调用全能方法
[self sd_setHighlightedImageWithURL:url options:options progress:nil completed:completedBlock];
}
复制代码
- (void)sd_setHighlightedImageWithURL:(nullable NSURL *)url
options:(SDWebImageOptions)options
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
completed:(nullable SDExternalCompletionBlock)completedBlock {
// 调用UIView+WebCache分类方法加载图像
__weak typeof(self)weakSelf = self;
[self sd_internalSetImageWithURL:url
placeholderImage:nil
options:options
operationKey:@"UIImageViewImageOperationHighlighted"
setImageBlock:^(UIImage *image, NSData *imageData) {
weakSelf.highlightedImage = image;
}
progress:progressBlock
completed:completedBlock];
}
复制代码
源码阅读系列:SDWebImage
源码阅读:SDWebImage(二)——SDWebImageCompat
源码阅读:SDWebImage(三)——NSData+ImageContentType
源码阅读:SDWebImage(四)——SDWebImageCoder
源码阅读:SDWebImage(五)——SDWebImageFrame
源码阅读:SDWebImage(六)——SDWebImageCoderHelper
源码阅读:SDWebImage(七)——SDWebImageImageIOCoder
源码阅读:SDWebImage(八)——SDWebImageGIFCoder
源码阅读:SDWebImage(九)——SDWebImageCodersManager
源码阅读:SDWebImage(十)——SDImageCacheConfig
源码阅读:SDWebImage(十一)——SDImageCache
源码阅读:SDWebImage(十二)——SDWebImageDownloaderOperation
源码阅读:SDWebImage(十三)——SDWebImageDownloader
源码阅读:SDWebImage(十四)——SDWebImageManager
源码阅读:SDWebImage(十五)——SDWebImagePrefetcher
源码阅读:SDWebImage(十六)——SDWebImageTransition
源码阅读:SDWebImage(十七)——UIView+WebCacheOperation
源码阅读:SDWebImage(十八)——UIView+WebCache
源码阅读:SDWebImage(十九)——UIImage+ForceDecode/UIImage+GIF/UIImage+MultiFormat
源码阅读:SDWebImage(二十)——UIButton+WebCache
源码阅读:SDWebImage(二十一)——UIImageView+WebCache/UIImageView+HighlightedWebCache