简单的模拟SDWebImage内部实现

SDWebImage是一个很厉害的图片缓存的框架

这里只简单的模拟一下SDWebImage简单的内部实现

思路是 : 第一次下载图片, 加载图片时, 将图片保存到本地沙盒和缓存中, 下次再请求同一张图片信息时, 先在缓存中取, 如果取不出来, 再到本地沙盒中取, 这时候如果还取不出来, 则从网络下载, 这样的好处在于可以避免网络图片重复下载, 节省用户流量

提供一个UIImageView类别以支持加载来自网络的远程图片。使其具有缓存管理、异步下载等特点

代码如下:

#import 

@interface UIImageView (Cache)

- (void)setImageWithURL:(NSURL *)url;
- (void)setImageWithURL:(NSURL *)url placeHolderImage:(UIImage *)image;

@end
#import "UIImageView+Cache.h"
#import 
#import "CacheManager.h"
@interface NSURL (md5)

- (NSString *)md5;

@end

@implementation NSURL (md5)

- (NSString *)md5
{
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(self.absoluteString.UTF8String, (unsigned int)strlen(self.absoluteString.UTF8String), result);
    NSMutableString *resultStr = [NSMutableString string];
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i ++) {
        [resultStr appendFormat:@"%02x",result[i]];
    }
    return resultStr;
}

@end


@implementation UIImageView (Cache)

- (void)setImageWithURL:(NSURL *)url
{
    [self setImageWithURL:url placeHolderImage:nil];
}
- (void)setImageWithURL:(NSURL *)url placeHolderImage:(UIImage *)image
{
    self.image = image;
    // 从缓存中加载
    __block NSData *imageData = [[CacheManager Cache] objectForKey:url.md5];
    if (imageData != nil)
    {
        NSLog(@"缓存加载图片");
        UIImage *img = [UIImage imageWithData:imageData];
        self.image = img;
        return;
    }
    // 从本地加载
    NSString *file = [[CacheManager cachePath] stringByAppendingPathComponent:url.md5];
    imageData = [NSData dataWithContentsOfFile:file];
    if (imageData != nil)
    {
        NSLog(@"从本地加载图片");
        UIImage *img = [UIImage imageWithData:imageData];
        self.image = img;
        [[CacheManager Cache] setObject:imageData forKey:url.md5];
        return;
    }
    // 从网络加载
    NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:[NSURLRequest requestWithURL:url] completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        NSURL *fileURL = [NSURL fileURLWithPath:file];
        [[NSFileManager defaultManager] copyItemAtURL:location toURL:fileURL error:nil];
        
        imageData = [NSData dataWithContentsOfURL:location];
        [[CacheManager Cache] setObject:imageData forKey:url.md5];
        NSLog(@"网络加载图片");
        dispatch_async(dispatch_get_main_queue(), ^{
            self.image = [UIImage imageWithData:imageData];
        });
    }];
    [task resume];
}

@end
#import 

@interface CacheManager : NSObject

+ (NSCache *)Cache;

+ (NSString *)cachePath;

+ (void)clearCache;

+ (unsigned long long)cacheBytesCount;

@end

#import "CacheManager.h"

static NSCache *s_imageCache = nil;
@implementation CacheManager

+ (NSCache *)Cache
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        s_imageCache = [[NSCache alloc] init];
        s_imageCache.name = @"imageCache";
    });
    return s_imageCache;
}

+ (NSString *)cachePath
{
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    return document;
}

+ (void)clearCache
{
    [[NSFileManager defaultManager] removeItemAtPath:[self cachePath] error:nil];
    [[NSFileManager defaultManager] createDirectoryAtPath:[self cachePath] withIntermediateDirectories:YES attributes:nil error:nil];
    [[self Cache] removeAllObjects];
}

+ (unsigned long long)cacheBytesCount
{
    NSFileManager *manager = [NSFileManager defaultManager];
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:[self cachePath]] objectEnumerator];
    NSString *fileName = nil;
    unsigned long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil) {
        NSString *fileAbsolutePath = [[self cachePath] stringByAppendingPathComponent:fileName];
        folderSize += [[manager attributesOfItemAtPath:fileAbsolutePath error:nil] fileSize]; 
    }
    return folderSize;
}
@end

SDWebImage远不止于此, 这里只是简单的模拟一下图片加载过程
Demo下载地址: https://github.com/gaoyuexit/DownLoadWebImage

你可能感兴趣的:(简单的模拟SDWebImage内部实现)