該文章閱讀的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