11?NSData+ImageContentType
+ (SDImageFormat)sd_imageFormatForImageData:(nullable NSData *)data {
if (!data) {
return SDImageFormatUndefined;
}
uint8_t c;
[data getBytes:&c length:1];
switch (c) {
case 0xFF:
return SDImageFormatJPEG;
case 0x89:
return SDImageFormatPNG;
case 0x47:
return SDImageFormatGIF;
case 0x49:
case 0x4D:
return SDImageFormatTIFF;
case 0x52:
// R as RIFF for WEBP
if (data.length < 12) {
return SDImageFormatUndefined;
}
NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(0, 12)] encoding:NSASCIIStringEncoding];
if ([testString hasPrefix:@"RIFF"] && [testString hasSuffix:@"WEBP"]) {
return SDImageFormatWebP;
}
}
return SDImageFormatUndefined;
}
這個(gè)就是根據(jù)文件頭判斷圖片格式甜紫。而不是根據(jù)后綴名。
12.UIView+WebCacheOperation
這個(gè)類三個(gè)public方法
1.- (void)sd_setImageLoadOperation:(nullable id)operation forKey:(nullable NSString *)key;
2.- (void)sd_cancelImageLoadOperationWithKey:(nullable NSString *)key;
3.- (void)sd_removeImageLoadOperationWithKey:(nullable NSString *)key;
先看
- (SDOperationsDictionary *)operationDictionary {
SDOperationsDictionary *operations = objc_getAssociatedObject(self, &loadOperationKey);
if (operations) {
return operations;
}
operations = [NSMutableDictionary dictionary];
objc_setAssociatedObject(self, &loadOperationKey, operations, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
return operations;
}
這個(gè)函數(shù)就是給uiview 增加一個(gè)關(guān)聯(lián)引用检眯,綁定一個(gè)字典。
- (void)sd_cancelImageLoadOperationWithKey:(nullable NSString *)key { // Cancel in progress downloader from queue SDOperationsDictionary *operationDictionary = [self operationDictionary]; id operations = operationDictionary[key]; if (operations) { if ([operations isKindOfClass:[NSArray class]]) { for (idoperation in operations) { if (operation) { [operation cancel]; } } } else if ([operations conformsToProtocol:@protocol(SDWebImageOperation)]){ [(id) operations cancel];
}
[operationDictionary removeObjectForKey:key];
}
}
先看cancel 函數(shù)
這個(gè)先獲取字典,根據(jù)key 找到operations 趾断。要是NSArray,就將數(shù)組里的對(duì)象(id<SDWebImageOperation>)調(diào)用cancel
要是id<SDWebImageOperation>對(duì)象就調(diào)用cancel函數(shù)兆蕉,最后從字典移除這個(gè)key
- (void)sd_setImageLoadOperation:(nullable id)operation forKey:(nullable NSString *)key {
if (key) {
[self sd_cancelImageLoadOperationWithKey:key];
if (operation) {
SDOperationsDictionary *operationDictionary = [self operationDictionary];
operationDictionary[key] = operation;
}
}
}
l這個(gè)函數(shù)就是講原來的key 對(duì)應(yīng)的數(shù)組全部取消掉。在重新復(fù)制key 和operation
- (void)sd_removeImageLoadOperationWithKey:(nullable NSString *)key {
if (key) {
SDOperationsDictionary *operationDictionary = [self operationDictionary];
[operationDictionary removeObjectForKey:key];
}
}
移除字典key
12.UIView+WebCache
這個(gè)類我們只看一個(gè)方法
- (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
由于這些都在以前的文章分析過缤沦。這里就簡單再回顧下虎韵,不貼代碼了。
1.取消operation 操作
2.將url 綁定到關(guān)聯(lián)引用上
3.要是沒有配置延遲加載placeholder 那就直接加載placeholder
4.有url ?缸废。檢查是否要設(shè)置指示器包蓝。有就增加指示器
5.加載圖片
6.將operation 綁定到uiview 綁定的字典里
7.數(shù)據(jù)返回,移除指示器企量。
8要是 self 銷毀了测萎,就return
9.要是有image 并且配置SDWebImageAvoidAutoSetImage 而且有completedBlock 就調(diào)用completedBlock
10 其他的判斷要是有image 調(diào)用方法- (void)sd_setImage:(UIImage *)image imageData:(NSData *)imageData basedOnClassOrViaCustomSetImageBlock:(SDSetImageBlock)setImageBloc
12.其他的,要是配置?SDWebImageDelayPlaceholder 也調(diào)用- (void)sd_setImage:(UIImage *)image imageData:(NSData *)imageData basedOnClassOrViaCustomSetImageBlock:(SDSetImageBlock)setImageBloc
13.要是finish 標(biāo)記為YES 那么調(diào)用completeBlock
13UIImageView+WebCache
這個(gè)里面其實(shí)沒啥調(diào)用可說的届巩,最后都調(diào)用到UIView中的方法绳泉。
14.UIImageView+HighlightedWebCache
給UIimageView 的heightImage賦值。
還有其他的類這里就不解釋姆泻。上面的類看懂了后面的這些就簡單很多零酪。