SDWebImage 框架的基本使用
git
首先要看readmegithub
這是個人翻譯web
這個庫提供了一個UIImageView的分類,能夠從網絡遠端加載圖片緩存
它提供了:網絡
一個UIImageView的分類加載網絡圖片和緩存管理框架
異步加載圖片異步
異步內存和磁盤緩存是自動緩存處理async
支持GIF測試
支持網頁格式this
支持背景圖片解壓縮
在一個期限內,相同的URL將不會被重複下載
錯誤的URL將不會被重試下載
在這個時間內,主線程將不會被阻礙
使用的是GCD和ARC
支持Arm64處理器
1.經過git 命令下載SDWebImage,這樣能夠獲得最新版本
git clone https://github.com/rs/SDWebImage.git
2.不知道官方文檔爲何使用尖括號導入,注意改爲雙引號
#import <SDWebImage/UIImageView+WebCache.h>
使用 UIImageView+WebCache 分類用於TableView
Just #import the UIImageView+WebCache.h header, and call the sd_setImageWithURL:placeholderImage: method from the tableView:cellForRowAtIndexPath: UITableViewDataSource method. Everything will be handled for you, from async downloads to caching management.
僅僅須要導入 UIImageView+WebCache.h 頭文件,而後調用sd_setImageWithURL:方法在tabelView的cellForRowAtIndexPath方法中使用,這是從網絡異步加載圖片到緩存的方法都實現了
#import "ViewController.h" // 導入頭文件的時候注意使用雙引號 #import "SDWebImage/UIImageView+WebCache.h" @interface ViewController ()<UITableViewDataSource> @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 10; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *MyIdentifier = @"MyIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier]; } // 這裏的圖片url要本身更換一下,佔位圖片也本身選擇一下 [cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://img1.3lian.com/img2008/05/003/023.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; cell.textLabel.text = @"My Text"; return cell; }
直接運行便可,測試運行效果
With blocks, you can be notified about the image download progress and whenever the image retrieval has completed with success or not:
neither your success nor failure block will be call if your image request is canceled before completion.
這是帶block的回調的方法,你能夠關注圖片下載進度和圖片是否下載成功
除非你的圖片請求被取消在調用block以前,不然不管block是否成功仍是失敗都將會被調用
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://img1.3lian.com/img2008/05/003/023.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { if(error) { NSLog(@"下載失敗"); }else { NSLog(@"下載成功"); } }]; 運行結果: SDWebImage使用[6118:399525] 下載成功 SDWebImage使用[6118:399525] 下載成功 SDWebImage使用[6118:399525] 下載成功 SDWebImage使用[6118:399525] 下載成功 SDWebImage使用[6118:399525] 下載成功 SDWebImage使用[6118:399525] 下載成功 SDWebImage使用[6118:399525] 下載成功 SDWebImage使用[6118:399525] 下載成功 SDWebImage使用[6118:399525] 下載成功 SDWebImage使用[6118:399525] 下載成功
使用單例管理類
The SDWebImageManager is the class behind the UIImageView+WebCache category. It ties the asynchronous downloader with the image cache store. You can use this class directly to benefit from web image downloading with caching in another context than a UIView (ie: with Cocoa).
Here is a simple example of how to use SDWebImageManager:
這個單例管理類是異步下載到圖片緩存都作了,
SDWebImageManager *manager = [SDWebImageManager sharedManager]; NSURL *imageURL = [NSURL URLWithString:@"http://img1.3lian.com/img2008/05/003/023.jpg"]; [manager downloadImageWithURL:imageURL options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) { NSLog(@"加載中 "); } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { if (image) { NSLog(@"圖片緩存完成"); } }]; // 打印沙盒目錄,查看Cache目錄下會有相應的圖片 NSString *path = NSHomeDirectory();//主目錄 NSLog(@"NSHomeDirectory:%@",path);