iOS開發SDWebImage的基本使用

來源:互聯網
上載者:User

標籤:

#import "ViewController.h"#import "UIImageView+WebCache.h"#import "SDWebImageManager.h"#import "SDWebImageDownloader.h"#import "UIImage+GIF.h"#import "NSData+ImageContentType.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet UIImageView *imageView;@end@implementation ViewController-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    [self download];}//1.下載圖片且需要擷取下載進度/** *    1:當需要擷取下載進度的時候可以使用此方法,其中optiobs如果什麼都不做,可以傳參數0,optiobs是一個位移枚舉,可以通過按位或 | 來並列添加參數 2:SDImageCacheType緩衝的類型:此方法預設做了記憶體緩衝,與磁碟緩衝,第一次是直接下載,然後點擊的時候是從記憶體緩衝中讀取,當記憶體緩衝不存在的時候,再從磁碟緩衝讀取 * *///記憶體緩衝&磁碟緩衝-(void)download{    [self.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://img4.duitang.com/uploads/blog/201310/18/20131018213446_smUw4.thumb.600_0.jpeg"] placeholderImage:[UIImage imageNamed:@"Snip20160221_306"] options:SDWebImageCacheMemoryOnly | SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize) {            } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {                switch (cacheType) {            case SDImageCacheTypeNone:                NSLog(@"直接下載");                break;            case SDImageCacheTypeDisk:                NSLog(@"磁碟緩衝");                break;            case SDImageCacheTypeMemory:                NSLog(@"記憶體緩衝");                break;            default:                break;        }    }];        NSLog(@"%@",[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]);    }//2.只需要簡單獲得一張圖片,不設定/** * 1:預設SDImageCacheType是記憶體緩衝&磁碟緩衝。如果只是簡單下載一張圖片,就用如下的方法:[SDWebImageManager sharedManager] downloadImageWithURL * */-(void)download2{    [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:@"http://img4.duitang.com/uploads/blog/201310/18/20131018213446_smUw4.thumb.600_0.jpeg"] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {        NSLog(@"%f",1.0 * receivedSize / expectedSize);            } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {                //得到圖片        self.imageView.image = image;    }];}//3.不需要任何的緩衝處理/** * 沒有做任何緩衝處理 * */-(void)download3{    //data:圖片的位元據    [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:@"http://img4.duitang.com/uploads/blog/201310/18/20131018213446_smUw4.thumb.600_0.jpeg"] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {            } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {                [[NSOperationQueue mainQueue]addOperationWithBlock:^{             self.imageView.image = image;        }];           }];}//4.播放Gif圖片/** *播放Gif圖片 */-(void)gif{    NSLog(@"%s",__func__);    //self.imageView.image = [UIImage imageNamed:@"39e805d5ad6eddc4f80259d23bdbb6fd536633ca"];        UIImage *image = [UIImage sd_animatedGIFNamed:@"39e805d5ad6eddc4f80259d23bdbb6fd536633ca"];    self.imageView.image = image;}-(void)type{    NSData *imageData = [NSData dataWithContentsOfFile:@"/Users/xiaomage/Desktop/Snip20160221_306.png"];    NSString *typeStr = [NSData sd_contentTypeForImageData:imageData];    NSLog(@"%@",typeStr);}@end
二:當記憶體產生警告的時候,清除緩衝
#import "AppDelegate.h"#import "SDWebImageManager.h"@interface AppDelegate ()@end@implementation AppDelegate-(void)applicationDidReceiveMemoryWarning:(UIApplication *)application{    //1.清空緩衝    //clear:直接刪除緩衝目錄下面的檔案,然後重新建立空的快取檔案    //clean:清除到期緩衝,計算當前緩衝的大小,和設定的最大緩衝數量比較,如果超出那麼會繼續刪除(按照檔案了建立的先後順序)    //到期時間:7天    [[SDWebImageManager sharedManager].imageCache clearMemory];        //2.取消當前所有的操作    [[SDWebImageManager sharedManager] cancelAll];        //3.最大並發數量 == 6    //4.快取檔案的儲存名稱如何處理? 拿到圖片的URL路徑,對該路徑進行MD5加密    //5.該架構內部對記憶體警告的處理方式? 內部通過監聽通知的方式請你緩衝    //6.該架構進行緩衝處理的方式:可變字典--->NSCache    //7.如何判斷圖片的類型: 在判斷圖片類型的時候,只匹配第一個位元組    //8.隊列中任務的處理方式:FIFO    //9.如何下載圖片的? 發送網路請求下載圖片,NSURLConnection    //10.請求逾時的時間 15秒        //[NSData dataWithContentsOfURL:<#(nonnull NSURL *)#>]}@end

三:SDWebImage的結構:最頂層的父類是SDWebImageManager,其下有兩個子類

 

 








四:知識點總結:


01 設定imageView的圖片 [cell.imageView sd_setImageWithURL:[NSURL URLWithString:app.icon] placeholderImage:[UIImage imageNamed:@"placehoder"]]; 02 設定圖片並計算下載進度 //下載並設定圖片 /* 第一個參數:要下載圖片的url地址 第二個參數:設定該imageView的佔位圖片 第三個參數:傳一個枚舉值,告訴程式你下載圖片的策略是什麼 第一個block塊:擷取當前圖片資料的下載進度 receivedSize:已經下載完成的資料大小 expectedSize:該檔案的資料總大小 第二個block塊:當圖片下載完成之後執行該block中的代碼 image:下載得到的圖片資料 error:下載出現的錯誤資訊 SDImageCacheType:圖片的緩衝策略(不緩衝,記憶體緩衝,沙箱緩衝) imageURL:下載的圖片的url地址 */ [cell.imageView sd_setImageWithURL:[NSURL URLWithString:app.icon] placeholderImage:[UIImage imageNamed:@"placehoder"] options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) { //計算當前圖片的下載進度 NSLog(@"%.2f",1.0 *receivedSize / expectedSize); } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { }]; 03 系統級記憶體警告如何處理(面試) //取消當前進行中的所有下載操作 [[SDWebImageManager sharedManager] cancelAll]; //清除快取資料(面試) //cleanDisk:刪除到期的檔案資料,計算當前未到期的已經下載的檔案資料的大小,如果發現該資料大小大於我們設定的最大快取資料大小,那麼程式內部會按照按檔案資料緩衝的時間從遠到近刪除,知道小於最大快取資料為止。 //clearMemory:直接刪除檔案,重新建立新的檔案夾 //[[SDWebImageManager sharedManager].imageCache cleanDisk]; [[SDWebImageManager sharedManager].imageCache clearMemory]; 04 SDWebImage預設的緩衝時間是1周 05 如何播放gif圖片 /* 5-1 把使用者傳入的gif圖片->NSData 5-2 根據該Data建立一個圖片資料來源(NSData->CFImageSourceRef) 5-3 計算該資料來源中一共有多少幀,把每一幀資料取出來放到圖片數組中 5-4 根據得到的數組+計算的動畫時間-》可動畫的image [UIImage animatedImageWithImages:images duration:duration]; */ 06 如何判斷當前圖片類型,只判斷圖片位元據的第一個位元組 + (NSString *)sd_contentTypeForImageData:(NSData *)data; 07 內部如何進行緩衝處理?使用了NSCache類,使用和NSDictionary類似 08 沙箱緩衝圖片的命名方式為對該圖片的URL進行MD5加密 echo -n "url" |MD5 09 當接收到記憶體警告之後,內部會自動清理記憶體緩衝 10 圖片的下載順序,預設是先進先出的:FIFO原則

iOS開發SDWebImage的基本使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.