美文网首页
SDWebImage大致原理

SDWebImage大致原理

作者: b470b9fc7145 | 来源:发表于2017-02-27 22:40 被阅读34次
    SDWebImage大致原理图

    源码

    
    UIImage *image = self.imageDictM[dataModel.icon];
        if (image == nil) {
            
                //拼接沙盒路径
                NSString *cahches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
                //文件名称
                NSString *fileName = [dataModel.icon lastPathComponent];
                
                //拼接文件全路径
                NSString *fullPath = [cahches stringByAppendingPathComponent:fileName];
                
                NSData *data = [NSData dataWithContentsOfFile:fullPath];
            if (data ==nil) {
                
                //设置占位图片
                cell.imageView.image = [UIImage imageNamed:@"Snip20151103_1"];
                
                //判断下载操作有没有进行
                NSBlockOperation *operation = self.operation[dataModel.icon];
                
                if (operation == nil) {
                    
                    //这个 block 需要时间慢慢运行完
                    NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{
                        
                        NSURL *url = [NSURL URLWithString:dataModel.icon];
                        NSData *data = [NSData dataWithContentsOfURL:url];
                        
                        UIImage *image = [UIImage imageWithData:data];
                        
                        if (image ==nil) {
                            //重字NSOperationQueue字典中移除任务
                            [self.operation removeObjectForKey:dataModel.icon];
                            return ;//这个断点的意思式这个 block,都不执行,
                        }
                        //保存图片给数组
                        [self.imageDictM setObject:image forKey:dataModel.icon];
                        
                        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
                            //刷新指定的行
                            [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
                            [tableView reloadData];
                        }];
                        
                        //下载完,把操作移除,下载完,就确认是数据下载成功,需要这时就可以写数据了
                        [data writeToFile:fullPath atomically:YES];
                        //将data写入沙盒
                        NSLog(@"++++++=");
                        [self.operation removeObjectForKey:dataModel.icon];
                        
                    }];
                    
                    //1.将动作添加到队列中,block创建后,方法队列中才会去执行
                    //执行就是创建任务
                    
                    [self.queue addOperation:operation1];
                    
                    //2.将动作添加到数组中
                    [self.operation setObject:operation1 forKey:dataModel.icon];
                    NSLog(@"------");
                }
                
            }
            
            else{
                       UIImage *image = [UIImage imageWithData:data];
                        //保存图片给数组
                        [self.imageDictM setObject:image forKey:dataModel.icon];
                        cell.imageView.image = image;
                        }
            
         }
        else{
            
            cell.imageView.image = image;
        }
    
    

    分块图解

    • block里面执行的情况

    框架的内存管理:

    当发生内存警告的时候如何处理
    1.取消当前正在下载的所有操作

     [[SDWebImageManager sharedManager]cancelAll];
    

    2.图片缓存处理-----这一句已经在 cancelAll 中已经实现了,就不需要我们再写

         kDefaultCacheMaxCacheAge:默认缓存周期是一周
         maxCacheSize:
         cleanDisk:找到缓存文件,先删除过期缓存,统计当前缓存文件的大小(currentSzie),如果发现大于最大缓存数量(currentSzie>maxCacheSize),那么会继续删除,按照缓存文件产生的事件顺序,从原道进删除,知道小于为止
         clearDisk:直接删除,然后创建新的文件夹
     
        //内部已经实现了
    //    [[SDWebImageManager sharedManager].imageCache clearDisk];----
    
    

    相关文章

      网友评论

          本文标题:SDWebImage大致原理

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