美文网首页面经
SDWebImage源码分析初解(一)

SDWebImage源码分析初解(一)

作者: James_Gong | 来源:发表于2018-07-12 14:21 被阅读26次

    SDWebImage的常见问题

    SDWebImage是我们开发中再常见不过的一个框架,包括在面试的时候,也经常会问到里面的一些常见问题.下面是对SDWebImage总结的常见面试题.
    我们先来了解下它的demo.本身从github下载下来的demo,直接运行的话,是会报错的.它会少一个叫做libwebp的一个文件夹,它是谷歌的一个库,那么想要运行这个demo的话,就得翻墙去下载这个库了.

    webp是谷歌开发的一种图片格式,它比我们常用的jpg这种图片格式的图片压缩体积大概小那么1/3左右. image.png

    常见的常识问题如下:

    1.SDWebImage的缓存机制
    2.它的最大并发数
    3.默认缓存周期是多久
    4.怎么辨别图片格式的

    SDWebImage的缓存机制

    本身SDWebImage它的核心主要是负责下载和缓存图片.缓存是由SDImageCache这个类来处理的.它有两种缓存方式,内存缓存和沙盒缓存.
    在内存方面,当接收到有内存警告的时候,它是由AutoPurgeCache这个类来处理的,这个类是继承自NSCache.当接收到内存警告的时候,会直接调用一个removeAllObjects这个方法来进行remove的.这是它对内存警告方面的处理.

    image.png

    在磁盘缓存上面的处理方式是通过调用这个cleanDiskWithCompletionBlock方法来进行处理的,根据它的缓存期限跟容量来删除一些图片数据.

    - (void)cleanDiskWithCompletionBlock:(SDWebImageNoParamsBlock)completionBlock {
        dispatch_async(self.ioQueue, ^{
            // 找到磁盘目录
            NSURL *diskCacheURL = [NSURL fileURLWithPath:self.diskCachePath isDirectory:YES];
            NSArray *resourceKeys = @[NSURLIsDirectoryKey, NSURLContentModificationDateKey, NSURLTotalFileAllocatedSizeKey];
    
            // This enumerator prefetches useful properties for our cache files.
            // 创建目录枚举
            NSDirectoryEnumerator *fileEnumerator = [_fileManager enumeratorAtURL:diskCacheURL
                                                       includingPropertiesForKeys:resourceKeys
                                                                          options:NSDirectoryEnumerationSkipsHiddenFiles
                                                                     errorHandler:NULL];
            // 从现在开始,减去最大缓存时间
            NSDate *expirationDate = [NSDate dateWithTimeIntervalSinceNow:-self.maxCacheAge];
           
            NSMutableDictionary *cacheFiles = [NSMutableDictionary dictionary];
            NSUInteger currentCacheSize = 0;
    
            // Enumerate all of the files in the cache directory.  This loop has two purposes:
            //
            //  1. Removing files that are older than the expiration date.
            //  2. Storing file attributes for the size-based cleanup pass.
            NSMutableArray *urlsToDelete = [[NSMutableArray alloc] init];
            
            //遍历所有的文件
            for (NSURL *fileURL in fileEnumerator) {
                NSDictionary *resourceValues = [fileURL resourceValuesForKeys:resourceKeys error:NULL];
    
                // Skip directories.
                // 遍历的时候, 发现是目录, 直接跳过
                if ([resourceValues[NSURLIsDirectoryKey] boolValue]) {
                    continue;
                }
    
                // Remove files that are older than the expiration date;
                // 判断文件日期,是否超过缓存日期,添加到数组里面
                NSDate *modificationDate = resourceValues[NSURLContentModificationDateKey];
                if ([[modificationDate laterDate:expirationDate] isEqualToDate:expirationDate]) {
                    [urlsToDelete addObject:fileURL];
                    continue;
                }
    
                // Store a reference to this file and account for its total size.
                // 记录文件大小
                NSNumber *totalAllocatedSize = resourceValues[NSURLTotalFileAllocatedSizeKey];
                currentCacheSize += [totalAllocatedSize unsignedIntegerValue];
                [cacheFiles setObject:resourceValues forKey:fileURL];
            }
            for (NSURL *fileURL in urlsToDelete) {
                [_fileManager removeItemAtURL:fileURL error:nil];
            }
    
            // If our remaining disk cache exceeds a configured maximum size, perform a second
            // size-based cleanup pass.  We delete the oldest files first.
            // 判断是否超过磁盘上限,默认是没有指定
            if (self.maxCacheSize > 0 && currentCacheSize > self.maxCacheSize) {
                // Target half of our maximum cache size for this cleanup pass.
                
                // 清除最大缓存的一半
                const NSUInteger desiredCacheSize = self.maxCacheSize / 2;
    
                // Sort the remaining cache files by their last modification time (oldest first).
                // 保留的缓存文件(最老的先删除)
                NSArray *sortedFiles = [cacheFiles keysSortedByValueWithOptions:NSSortConcurrent
                                                                usingComparator:^NSComparisonResult(id obj1, id obj2) {
                                                                    return [obj1[NSURLContentModificationDateKey] compare:obj2[NSURLContentModificationDateKey]];
                                                                }];
    
                // Delete files until we fall below our desired cache size.
                // 删除文件.到指定的缓存大小
                for (NSURL *fileURL in sortedFiles) {
                    if ([_fileManager removeItemAtURL:fileURL error:nil]) {
                        NSDictionary *resourceValues = cacheFiles[fileURL];
                        NSNumber *totalAllocatedSize = resourceValues[NSURLTotalFileAllocatedSizeKey];
                        currentCacheSize -= [totalAllocatedSize unsignedIntegerValue];
    
                        if (currentCacheSize < desiredCacheSize) {
                            break;
                        }
                    }
                }
            }
            if (completionBlock) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    completionBlock();
                });
            }
        });
        }
    

    最大并发数

    SDWebImageDownloader这个类里面的init方法里面,设置了它的最大并发数是6

    _downloadQueue.maxConcurrentOperationCount = 6; 
    

    默认缓存周期是多久

    在SDImageCache这个里面设置了它的最大缓存周期是7天

    static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
    

    怎么辨别图片格式的

    在NSData+ImageContentType这个类里面实现了这样的一个方法
    sd_contentTypeForImageData:(NSData *)data
    里面传入图片的二进制数据,获取图片的第一个字节.以第一个字节为例来辨别图片的格式

    相关文章

      网友评论

        本文标题:SDWebImage源码分析初解(一)

        本文链接:https://www.haomeiwen.com/subject/iexrpftx.html