美文网首页
SDWebImage的基本用法及常见问题

SDWebImage的基本用法及常见问题

作者: 褪而未变 | 来源:发表于2017-07-24 20:09 被阅读0次

    SDWebImage的基本用法

    1.框架集成

    • CocoaPods集成 : pod 'SDWebImage'
    • 若要加载GIF图 : pod 'SDWebImage/GIF'

    2.准备工作

    * 加载静态图导入头文件 `#import <UIImageView+WebCache.h>`
    * 加载GIF图导入头文件 `#import <FLAnimatedImageView+WebCache.h>`
    * 进度指示器导入头文件 : `#import <UIView+WebCache.h>`
    * 准备控件
        * 展示静态图 `@property (weak, nonatomic) IBOutlet UIImageView *imgView;`
        * 展示GIF/静态图 `@property (weak, nonatomic) IBOutlet FLAnimatedImageView *imgView;`
    

    3.加载GIF图

    - (void)loadGIF {
        // 加载网络gif图
        NSURL *URL = [NSURL URLWithString:@"http://image.nihaowang.com/news/2015-04-27/c30f866d-9300-4f6e-86f6-58f408630e14.gif"];
    
        [self.imgView sd_setImageWithURL:URL];
    }
    

    4.监听图片下载进度

    - (void)loadProgress {
    
        NSURL *URL = [NSURL URLWithString:@"http://img.taopic.com/uploads/allimg/140806/235020-140P60H10661.jpg"];
    ---------------------------------------------------------------------------------
    //用此方法下载,SDWebImage会自动检查图片是否已经缓存,如已经缓存则从缓存位置读取,否则从网络下载,读取顺序是: 内存缓存->磁盘缓存->网络加载
        //个人理解此方法意思为*加载图片*
        [self.imgView sd_setImageWithURL:URL placeholderImage:nil options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
    
            // receivedSize : 接收的图片大小;expectedSize : 图片的总大小
            float progress = (float)receivedSize / expectedSize;
            NSLog(@"%f %@",progress,targetURL);
    
        } completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            NSLog(@"图片下载完成 %@",image);
        }];
    }
    ---------------------------------------------------------------------------------
    //用此方法下载,SDWebImage不会自动检查图片是否已经缓存,每次执行重新下载图片
        //个人理解此方法意思为*下载图片*
        [[SDWebImageManager sharedManager].imageDownloader downloadImageWithURL:url options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
            NSLog(@"下载的进度:%zd / %zd", receivedSize, expectedSize);
        } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
            [data writeToFile:@"/Users/apple/Desktop/aaa.png" atomically:true];
            NSLog(@"%@", NSStringFromCGSize(image.size));
        }];
    
    

    options所有枚举值:

        //失败后重试
         SDWebImageRetryFailed = 1 << 0,
          
         //UI交互期间开始下载,导致延迟下载比如UIScrollView减速。
         SDWebImageLowPriority = 1 << 1,
          
         //只进行内存缓存
         SDWebImageCacheMemoryOnly = 1 << 2,
          
         //这个标志可以渐进式下载,显示的图像是逐步在下载
         SDWebImageProgressiveDownload = 1 << 3,
          
         //刷新缓存
         SDWebImageRefreshCached = 1 << 4,
          
         //后台下载
         SDWebImageContinueInBackground = 1 << 5,
          
         //NSMutableURLRequest.HTTPShouldHandleCookies = YES;
          
         SDWebImageHandleCookies = 1 << 6,
          
         //允许使用无效的SSL证书
         //SDWebImageAllowInvalidSSLCertificates = 1 << 7,
          
         //优先下载
         SDWebImageHighPriority = 1 << 8,
          
         //延迟占位符
         SDWebImageDelayPlaceholder = 1 << 9,
          
         //改变动画形象
         SDWebImageTransformAnimatedImage = 1 << 10,
    

    5.进度指示器

    • 导入头文件 : #import <UIView+WebCache.h>
    - (void)loadProgressIndicator {
    
        // http://img.taopic.com/uploads/allimg/140806/235017-140P60PG685.jpg
        NSURL *URL = [NSURL URLWithString:@"http://img.taopic.com/uploads/allimg/140126/235002-14012609350290.jpg"];
    
        // 展示进度指示器
        [self.imgView sd_setShowActivityIndicatorView:YES];
        [self.imgView sd_setIndicatorStyle:UIActivityIndicatorViewStyleGray];
    
        [self.imgView sd_setImageWithURL:URL];
    }
    

    6.Manager下载图片

    - (void)LoadImageWithManager {
        NSURL *URL = [NSURL URLWithString:@"http://pic37.nipic.com/20140209/8821914_163234218136_2.jpg"];
    
        [[SDWebImageManager sharedManager] loadImageWithURL:URL options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
    
            // receivedSize : 接收的图片大小;expectedSize : 图片的总大小
            float progress = (float)receivedSize / expectedSize;
            NSLog(@"%f %@",progress,targetURL);
    
        } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
            // 图片赋值
            self.imgView.image = image;
        }];
    }
    

    SD常见问题

    1> 图片文件缓存的时间有多长:1周

    _maxCacheAge = kDefaultCacheMaxCacheAge

    2> SDWebImage 的内存缓存是用什么实现的?

    NSCache

    3> SDWebImage 的最大并发数是多少?

    maxConcurrentDownloads = 6

    4> SDWebImage 支持动图吗?GIF

    //SDWebImage4.0以后改用此方法,性能更加
    #import <FLAnimatedImageView+WebCache.h>
    [self.imageView sd_setImageWithURL:url];
    ------------------------------------------------------------
    //SDWebImage4.0以前使用此方法
    #import <ImageIO/ImageIO.h>
    [UIImage animatedImageWithImages:images duration:duration];
    

    5> SDWebImage是如何区分不同格式的图像的

    • 根据图像数据第一个字节来判断的!
      • PNG:0x89
      • JPG:0xFF
      • GIF:0x47


    6> SDWebImage 缓存图片的名称是怎么确定的!

    • md5
      • 如果单纯使用 文件名保存,重名的几率很高!
      • 使用 MD5 的散列函数!对完整的 URL 进行 md5,结果是一个 32 个字符长度的字符串!

    7> SDWebImage 的内存警告是如何处理的!

    • 利用通知中心观察
    • - UIApplicationDidReceiveMemoryWarningNotification 接收到内存警告的通知
      • 执行 clearMemory 方法,清理内存缓存!
    • - UIApplicationWillTerminateNotification 接收到应用程序将要终止通知
      • 执行 cleanDisk 方法,清理磁盘缓存!
    • - UIApplicationDidEnterBackgroundNotification 接收到应用程序进入后台通知
      • 执行 backgroundCleanDisk 方法,后台清理磁盘!
      • 通过以上通知监听,能够保证缓存文件的大小始终在控制范围之内!
      • clearDisk 清空磁盘缓存,将所有缓存目录中的文件,全部删除!
        实际工作,将缓存目录直接删除,再次创建一个同名空目录!

    相关文章

      网友评论

          本文标题:SDWebImage的基本用法及常见问题

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