美文网首页AFNetworking 3.0
AFN 3.0学习总结(七)

AFN 3.0学习总结(七)

作者: 油麦菜洋葱头 | 来源:发表于2018-01-14 00:18 被阅读25次

    参考:AFNetworking 3.0 源码解读(七)之 AFAutoPurgingImageCache

    说明:很多内容都是摘抄原文,只是根据自己的需要进行摘抄或者总结,如有不妥请及时指出,谢谢。

    AFImageCache

    通过这个协议,我们能够做下边四件事:


    image

    AFImageRequestCache

    这个协议继承自AFImageCache,然后又扩展了下边三个方法:


    image

    AFAutoPurgingImageCache

    继承AFImageRequestCache,额外增加了几个属性和部分初始化方法


    AFCachedImage

    image.png

    由此可知图片大小的计算方式:
    大小=像素个数✖️像素所占的字节数
    像素个数=长✖️宽
    每个像素的字节数=4
    所以:大小=长✖️宽✖️4


    AFAutoPurgingImageCache

    既然是图片的临时缓存类,那么我们应该把图片缓存到什么地方呢?答案就是一个字典中。值得注意的是,我们缓存使用的是一个同步的队列 。

    NSMutableDictionary <NSString* , AFCachedImage*> *cachedImages 存放图片
    UInt64 currentMemoryUsage 当前使用的容量
    dispatch_queue_t synchronizationQueue 队列

    - (instancetype)init {
    return [self initWithMemoryCapacity:100 * 1024 * 1024 preferredMemoryCapacity:60 * 1024 * 1024];
    

    }
    通过这个方法我们能够看出,缓存的默认空间大小为100M,清除后的大小为60M

    - (void)addImage:(UIImage *)image withIdentifier:(NSString *)identifier {
    dispatch_barrier_async(self.synchronizationQueue, ^{
        //创建AFCachedImage
        AFCachedImage *cacheImage = [[AFCachedImage alloc] initWithImage:image identifier:identifier];
    
        //根据identifier判断缓存中是否存在该图片,存在则更新使用空间大小
        AFCachedImage *previousCachedImage = self.cachedImages[identifier];
        if (previousCachedImage != nil) {
            self.currentMemoryUsage -= previousCachedImage.totalBytes;
        }
    
        //添加到缓存中
        self.cachedImages[identifier] = cacheImage;
        self.currentMemoryUsage += cacheImage.totalBytes;
    });
    
    dispatch_barrier_async(self.synchronizationQueue, ^{
        //如果使用空间超出了上线,则要进行数据清除
        if (self.currentMemoryUsage > self.memoryCapacity) {
            //计算要腾出来的空间大小
            UInt64 bytesToPurge = self.currentMemoryUsage - self.preferredMemoryUsageAfterPurge;
            //对缓存图片进行排序,按照最后访问时间生序排列
            NSMutableArray <AFCachedImage*> *sortedImages = [NSMutableArray arrayWithArray:self.cachedImages.allValues];
            NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastAccessDate"
                                                                           ascending:YES];
            [sortedImages sortUsingDescriptors:@[sortDescriptor]];
    
            UInt64 bytesPurged = 0;
    
            //删除比较早期的图片,知道空间大小满足条件
            for (AFCachedImage *cachedImage in sortedImages) {
                [self.cachedImages removeObjectForKey:cachedImage.identifier];
                bytesPurged += cachedImage.totalBytes;
                if (bytesPurged >= bytesToPurge) {
                    break ;
                }
            }
            self.currentMemoryUsage -= bytesPurged;
        }
    });
    }
    

    这是图片缓存的核心方法,总共做了两件事
    1、把图片添加到缓存字典中,然后更新空间大小
    2、处理空间大小超过设定上限(100M)的异常情况

    1)比较当前的容量是否超出了上限
    2)计算要清除的图片的大小
    3)把所有图片都放到一个数组中
    4)对这个数组按照最后访问时间进行升序排序,优先删除时间比较早的图片
    5)遍历数组移除图片,当删除图片总大小大于等于要删除总大小为止
    

    上面函数中用到了dispatch_barrier_async,这个意思是等它之前添加的任务完成后,再执行它,举例如下:

    dispatch_queue_t concurrentQueue = dispatch_queue_create("my.concurrent.queue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(concurrentQueue, ^(){
        NSLog(@"1");
    });
    dispatch_async(concurrentQueue, ^(){
        NSLog(@"2");
    });
    dispatch_barrier_async(concurrentQueue, ^(){
        NSLog(@"barrier");
    });
    dispatch_async(concurrentQueue, ^(){
        NSLog(@"3");
    });
    dispatch_async(concurrentQueue, ^(){
        NSLog(@"4");
    });
    

    输出结果为:1、2、barrier、3、4,其中1和2,3和4的顺序不固定

    相关文章

      网友评论

        本文标题:AFN 3.0学习总结(七)

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