iOS網絡編程(六):SDWebImage的簡單使用

SDWebImage一些知識:

  • 對遠程圖片進行緩存管理(磁盤+內存)
  • 異步加載
  • 同一個URL圖片不會重復下載
  • 失效的URL不會被無限重試

圖片加載一般使用

  • SDWebImage通過為UIImageView添加類別,擴展了一些類似的方法,只是參數的多少數目不同,最全的參數的方法是:

      -sd_setImageWithURL:placeholderImage:options:progress:completed:
    
    • sd_setImageWithURL: URL的圖片的URL
    • placeholderImage: 遠程圖片沒有加載完成的占位圖片
    • options: 一個位移枚舉(SDWebImageOptions),可以通過按位或 | 來并列添加參數。SDWebImageOptions的所有選項如下
            //失敗后重試
            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,
      
    • progress: Block 獲取當前圖片數據的下載進度
      receivedSize:已經下載完成的數據大小
      expectedSize:該文件的數據總大小
    • completed: Block加載完
      image:下載得到的圖片數據
      error:下載出現的錯誤信息
      SDImageCacheType:圖片的緩存策略
      imageURL:下載的圖片的url地址
  • 引入UIImageView+WebCache類別就可以使用SDWebImage加載圖片
#import "UIImageView+WebCache.h"
  • 不同參數方法使用??
    [self.imageView sd_setImageWithURL: url];
    
    [self.imageView sd_setImageWithURL: url completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        NSLog(@"圖片加載完");
    }];
    
    [self.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"apple"]];
    
    [self.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"apple"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        NSLog(@"圖片加載完");
    }];
    
    [self.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"apple"] options: SDWebImageCacheMemoryOnly];

    [self.imageView sd_setImageWithURL:url placeholderImage:nil options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
        //計算當前圖片的下載進度
        NSLog(@"下載進度:%.2f",1.0 *receivedSize / expectedSize);
    } completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
    }];

圖片加載過程

緩存大小

    NSInteger *cacheSize = [[SDImageCache sharedImageCache] getSize];
    NSLog(@"緩存大小:%d", cacheSize);

清理緩存

// 清理內存緩存
[[SDImageCache sharedImageCache] clearMemory];

// 清理磁盤緩存
[[SDImageCache sharedImageCache] clearDisk];

查看緩存完整目錄

NSLog(@"%s__%d__|%@",__FUNCTION__,__LINE__,[[SDImageCache sharedImageCache] defaultCachePathForKey:@"http://img4.duitang.com/uploads/blog/201310/18/20131018213446_smUw4.thumb.600_0.jpeg"]);

代碼:SDWebImageDemo

參考:
iOS圖片加載框架-SDWebImage解讀
SDWebImage 的簡單使用方法
iOS開發SDWebImage的基本使用

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容