1.NSDta (ImageContentType) 这个分类就一个方法
+ (SDImageFormat)sd_imageFormatForImageData:(nullableNSData *)data {
if (!data) {
returnSDImageFormatUndefined;
}
uint8_t c;//(uint8_t:无符号char)
[data getBytes:&clength:1];//取第一个字节判断图片类型.即图片真正类型不看你是点.png还是.jpg结尾,而是图片的二进制数据第一个字节具有唯一性.即不同类型的图片二进制数据第一个字节不同.sd作者收集的图片暂且如下,不能确定是否包含了全部图片
switch (c) {
case0xFF:
returnSDImageFormatJPEG;
case0x89:
returnSDImageFormatPNG;
case0x47:
returnSDImageFormatGIF;
case0x49:
case0x4D:
returnSDImageFormatTIFF;
case0x52:
// R as RIFF for WEBP
if (data.length <12) {
returnSDImageFormatUndefined;
}
NSString *testString = [[NSStringalloc]initWithData:[datasubdataWithRange:NSMakeRange(0,12)]encoding:NSASCIIStringEncoding];
if ([testStringhasPrefix:@"RIFF"] && [testStringhasSuffix:@"WEBP"]) {
returnSDImageFormatWebP;
}
}
returnSDImageFormatUndefined;
}
2.SDWebImageCompat 这个类让我好好看了系统了两个宏定义头文件
<Target Conditionals> <Availability>(牵连出来的)
#if __IPHONE_OS_VERSION_MIN_REQUIRED != 20000 && __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_5_0
#error SDWebImage doesn't support Deployment Target version < 5.0
#endif
这就限制了sd代码只能运行在ios5.0及以后的版本.不然直接报错 (#error用法类似#warning)#if !TARGET_OS_IPHONE && !TARGET_OS_IOS && !TARGET_OS_TV && !TARGET_OS_WATCH
#define SD_MAC 1
#else
#define SD_MAC 0
#endif
3.dispatch_main_async_safe//到主线程去执行block
#ifndef dispatch_main_async_safe
#define dispatch_main_async_safe(block)\
if (strcmp(dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL), dispatch_queue_get_label(dispatch_get_main_queue())) ==0) {\
block();\ //比较当前线程的标签和主线程的标签,如果相同执行执行block
} else {\ //如果不同,则跳到主线程执行.
dispatch_async(dispatch_get_main_queue(), block);\
}
#endif
4.CGContextSetInterpolationQuality(destContext,kCGInterpolationHigh);
typedef CF_ENUM (int32_t, CGInterpolationQuality) {
kCGInterpolationDefault = 0, /* Let the context decide. */
kCGInterpolationNone = 1, /* Never interpolate. */
kCGInterpolationLow = 2, /* Low quality, fast interpolation. */
kCGInterpolationMedium = 4, /* Medium quality, slower than kCGInterpolationLow. */
kCGInterpolationHigh = 3 /* Highest quality, slower than kCGInterpolationMedium. */
};
5.SDWebImageDownloader里面的循环引用
self.session = [NSURLSessionsessionWithConfiguration:sessionConfiguration
delegate:self
delegateQueue:nil];
我们知道NSURLSession对象的delegate属性是retain的
@property (nullable,readonly,retain)id <NSURLSessionDelegate> delegate;
所以初始化session之后已经是循环引用了,在没有打破这个循环引用的情况下,delloc方法根本不会被执行,所以SDWebImageDownloader没有提供打破循环引用接口时,这段delloc代码就显得多余了.- (void)dealloc {
[self.sessioninvalidateAndCancel];
self.session =nil;
[self.downloadQueuecancelAllOperations];
SDDispatchQueueRelease(_barrierQueue);
}