zoukankan      html  css  js  c++  java
  • SDWebImage缓存下载图片

    1.刚刚回去看了看SDWebImage缓存下载图片的东西 感觉用到的地方非常多,所以感觉有必要记录下来和大家分享一下

    下面是核心代码和注释:之前需要通过cocoaPods或者手动导入SDWebImage

     //获取模型
        App *app = self.dataArray[indexPath.row];
        cell.textLabel.text = app.name;
        cell.detailTextLabel.text = app.download;
        NSURL *url = [NSURL URLWithString:app.icon];
        
        //利用SDWebImage 缓存图片 但是需要处理内存警告
        [cell.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"0.png"] options:SDWebImageRetryFailed | SDWebImageLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            NSLog(@"%.2f",(double)receivedSize / expectedSize);
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            NSLog(@"加载完毕");
        }];

    这个方法粘贴出来是这样:

    - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock;

    注释:第一个参数是图片的网址,第二个参数是占位图,第三个图片是下载图片的选项,通常用这些:

    SDWebImageRetryFailed //下载失败就会一直下载图片

    SDWebImageLowPriority //在用户交互的时候 就会暂停下载图片 低优先级

    SDWebImageProgressiveDownload //从上往下下载 

    //**2.然后在AppDelegate.m文件中需要进行内存警告的处理  需要导入SDWebImageManager 这个类

    重写这个方法:

    /**
     当app收到内存警告时
     */
    
    - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
        //1.取消正在下载的任务  是单例
        SDWebImageManager *manager = [SDWebImageManager sharedManager];
        [manager cancelAll];
        //2.清除缓存就行了 需要manager.imageCache
        [manager.imageCache clearMemory];
    
    }

    //ok!

     

     

     

     

     

     

     

  • 相关阅读:
    centos 7 nginx 安装
    搭建Nuget.Server push时,"Failed to process request. 'Method Not Allowed'"
    Failed to create prime the NuGet cache
    Centos 7 安装 Visual stdio Code
    diskpart 格式化u盘 制作u盘启动盘方法
    sql server 2012 数据库日志文件过大,怎么缩小?
    浏览器同源政策及其规避方法
    redis底层数据结构--简单动态字符串 链表 字典 跳跃表 整数集合 压缩列表
    php的闭包
    hash一致性算法
  • 原文地址:https://www.cnblogs.com/arenouba/p/5306553.html
Copyright © 2011-2022 走看看