SDWebImage源码解析

2016-07-12  本文已影响98人  百省

SDWebImage中核心类如下所示:

26E06FD9-3B94-4AB4-9375-A2E7E5CC4470副本.png

一、SDImageCache
SDImageCache负责缓存图片,包含几个对外属性

//是否解压图片
@property (assign, nonatomic) BOOL shouldDecompressImages;
//iCloud 备份
@property (assign, nonatomic) BOOL shouldDisableiCloud;
//是否能够缓存图片到内存
@property (assign, nonatomic) BOOL shouldCacheImagesInMemory;
//最大内存使用,内存
@property (assign, nonatomic) NSUInteger maxMemoryCost;
//缓存对象的数量最大值
@property (assign, nonatomic) NSUInteger maxMemoryCountLimit;
//缓存时间,默认一周
@property (assign, nonatomic) NSInteger maxCacheAge;
//缓存最大值,磁盘
@property (assign, nonatomic) NSUInteger maxCacheSize;

内部属性:

//缓存对象
@property (strong, nonatomic) NSCache *memCache;
//磁盘缓存路径
@property (strong, nonatomic) NSString *diskCachePath;
//自定义路径数组,如bundle里的图片,可以添加到该数组中
@property (strong, nonatomic) NSMutableArray *customPaths;
//读写队列
@property (SDDispatchQueueSetterSementics, nonatomic) dispatch_queue_t ioQueue;

AutoPurgeCache,当收到内存警告时自动清空缓存

//初始化类的成员变量,监听内存警告、APP退到后台、APP终止通知

<pre><code>

if TARGET_OS_IPHONE

            //获取图片的alpha信息,判断是否是png图片
            int alphaInfo = CGImageGetAlphaInfo(image.CGImage);
            BOOL hasAlpha = !(alphaInfo == kCGImageAlphaNone ||
                              alphaInfo == kCGImageAlphaNoneSkipFirst ||
                              alphaInfo == kCGImageAlphaNoneSkipLast);
            BOOL imageIsPng = hasAlpha;
            
            if ([imageData length] >= [kPNGSignatureData length]) {
                imageIsPng = ImageDataHasPNGPreffix(imageData);
            }
            
            if (imageIsPng) {
                data = UIImagePNGRepresentation(image);
            }
            else {
                data = UIImageJPEGRepresentation(image, (CGFloat)1.0);
            }

else

            data = [NSBitmapImageRep representationOfImageRepsInArray:image.representations usingType: NSJPEGFileType properties:nil];

endif

        }            
        // 把data写入磁盘
        if (data) {
            if (![_fileManager fileExistsAtPath:_diskCachePath]) {
                [_fileManager createDirectoryAtPath:_diskCachePath withIntermediateDirectories:YES attributes:nil error:NULL];
            }
            
            // get cache Path for image key
            NSString *cachePathForKey = [self defaultCachePathForKey:key];
            // transform to NSUrl
            NSURL *fileURL = [NSURL fileURLWithPath:cachePathForKey];
            
            [_fileManager createFileAtPath:cachePathForKey contents:data attributes:nil];
            
            // disable iCloud backup
            if (self.shouldDisableiCloud) {
                [fileURL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:nil];
            }
        }
    });
}

}
</code></pre>

//查询图片是否已存在

  • (BOOL)diskImageExistsWithKey:(NSString *)key;
  • (void)diskImageExistsWithKey:(NSString *)key completion:(SDWebImageCheckCacheCompletionBlock)completionBlock;

//从缓存中取图片

<pre><code>//在磁盘中查找图片

//从缓存中移除图片,

}
}

if (completionBlock) {

dispatch_async(dispatch_get_main_queue(), ^{

completionBlock(); }); } });
}

// 获取已缓存文件的总容量

// 获取已缓存图片的总数量

// 计算文件大小及文件数量

上一篇下一篇

猜你喜欢

热点阅读