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;
}
这个就是根据文件头判断图片格式。而不是根据后缀名。
12.UIView+WebCacheOperation
这个类三个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;
}
这个函数就是给uiview 增加一个关联引用,绑定一个字典。
- (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 函数
这个先获取字典,根据key 找到operations 。要是NSArray,就将数组里的对象(id
要是id
- (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这个函数就是讲原来的key 对应的数组全部取消掉。在重新复制key 和operation
- (void)sd_removeImageLoadOperationWithKey:(nullable NSString *)key {
if (key) {
SDOperationsDictionary *operationDictionary = [self operationDictionary];
[operationDictionary removeObjectForKey:key];
}
}
移除字典key
12.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
由于这些都在以前的文章分析过。这里就简单再回顾下,不贴代码了。
1.取消operation 操作
2.将url 绑定到关联引用上
3.要是没有配置延迟加载placeholder 那就直接加载placeholder
4.有url 。检查是否要设置指示器。有就增加指示器
5.加载图片
6.将operation 绑定到uiview 绑定的字典里
7.数据返回,移除指示器。
8要是 self 销毁了,就return
9.要是有image 并且配置SDWebImageAvoidAutoSetImage 而且有completedBlock 就调用completedBlock
10 其他的判断要是有image 调用方法- (void)sd_setImage:(UIImage *)image imageData:(NSData *)imageData basedOnClassOrViaCustomSetImageBlock:(SDSetImageBlock)setImageBloc
12.其他的,要是配置 SDWebImageDelayPlaceholder 也调用- (void)sd_setImage:(UIImage *)image imageData:(NSData *)imageData basedOnClassOrViaCustomSetImageBlock:(SDSetImageBlock)setImageBloc
13.要是finish 标记为YES 那么调用completeBlock
13UIImageView+WebCache
这个里面其实没啥调用可说的,最后都调用到UIView中的方法。
14.UIImageView+HighlightedWebCache
给UIimageView 的heightImage赋值。
还有其他的类这里就不解释。上面的类看懂了后面的这些就简单很多。