前兩篇是自己寫的下載圖片方法,現在用第三方框架只要幾行代碼就可以實現圖片的下載。SDWebImage底層實現的思路也是和前面說的一樣。 SDWebImage是網絡圖片處理框架,封裝很很多方法,例如:圖片下載、圖片緩存、下載進度監聽、gif處理等等。大大提高了網絡圖片處理的效率。值得使用
。
github托管地址:[https://github.com/rs/SDWebImage](https://github.com/rs/SDWebImage)
實現cell圖片下載之前,先了解一下SDWebImage的使用:
1、導入框架,引入頭文件:
~~~
#import "UIImageView+WebCache.h"
~~~
2、圖片下載并緩存方法:
UIImageView+WebCache.h
~~~
//得到當前圖片的url
- (NSURL *)sd_imageURL;
/**
* 異步下載圖片并緩存
*/
- (void)sd_setImageWithURL:(NSURL *)url;
/**
* 異步下載圖片并緩存,沒下載完之前先顯示占位圖片,下載完之后再替換
*/
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder;
/**
* 異步下載圖片并緩存
* @param url 下載圖片路徑
* @param placeholder 占位圖片,直到下載完成才替換
* @param options 下載圖片選擇方式
*/
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options;
/**
* 異步下載圖片并緩存,完成后可以在block中做事情
* @param url 下載圖片url
* @param completedBlock
SDWebImageCompletionBlock:UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL)
block中可以得到下載圖片,錯誤信息,緩存類型,下載圖片地址 參數,給用戶做相應操作
*/
- (void)sd_setImageWithURL:(NSURL *)url completed:(SDWebImageCompletionBlock)completedBlock;
/**
* 異步下載圖片并緩存,提供占位圖片,并完成后可以在block中做事情
*
* @param url T下載圖片url
* @param placeholder T占位圖片,直到下載完成才替換
* @param completedBlock
SDWebImageCompletionBlock:UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL)
block中可以得到下載圖片,錯誤信息,緩存類型,下載圖片地址 參數,給用戶做相應操作
*/
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletionBlock)completedBlock;
/**
* 異步下載圖片并緩存,完成后可以在block中做事情
* @param url 下載圖片路徑
* @param placeholder 占位圖片,直到下載完成才替換
* @param options 下載圖片選擇方式
* @param completedBlock 同上
*/
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock;
/**
* 異步下載圖片并緩存,可以監聽下載進度,完成后可以在block中做事情
* @param url 下載圖片路徑
* @param placeholder 占位圖片,直到下載完成才替換
* @param options 下載圖片選擇方式
* @param progress
* SDWebImageDownloaderProgressBlock:NSInteger receivedSize(當前下載大小), NSInteger expectedSize(總大小)
* @param completedBlock 同上
*/
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock;
/**
* 異步下載圖片并緩存,可以監聽下載進度,完成后可以在block中做事情
* @param url 下載圖片路徑
* @param placeholder 占位圖片,直到下載完成才替換
* @param options 下載圖片選擇方式
* @param progress
* SDWebImageDownloaderProgressBlock:NSInteger receivedSize(當前下載大小), NSInteger expectedSize(總大小)
* @param completedBlock 同上
*/
- (void)sd_setImageWithPreviousCachedImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock;
~~~
3、options所有選項
~~~
//失敗后重新下載
SDWebImageRetryFailed = 1 << 0,
//最低優先級,當正在進行UI交互時,自動暫停內部的一些下載操作
SDWebImageLowPriority = 1 << 1,
//只緩存內存
SDWebImageCacheMemoryOnly = 1 << 2,
//漸進式下載,顯示的圖像是逐步在下載
SDWebImageProgressiveDownload = 1 << 3,
//刷新緩存
SDWebImageRefreshCached = 1 << 4,
//后臺下載
SDWebImageContinueInBackground = 1 << 5,
/**
* Handles cookies stored in NSHTTPCookieStore by setting
* NSMutableURLRequest.HTTPShouldHandleCookies = YES;
*/
SDWebImageHandleCookies = 1 << 6,
//允許使用無效的SSL證書
SDWebImageAllowInvalidSSLCertificates = 1 << 7,
//高優先級下載
SDWebImageHighPriority = 1 << 8,
//延遲占位符
SDWebImageDelayPlaceholder = 1 << 9,
//改變動畫形象
SDWebImageTransformAnimatedImage = 1 << 10,
/**
* By default, image is added to the imageView after download. But in some cases, we want to
* have the hand before setting the image (apply a filter or add it with cross-fade animation for instance)
* Use this flag if you want to manually set the image in the completion when success
*/
SDWebImageAvoidAutoSetImage = 1 << 11
~~~
4、內存處理:當app接收到內存警告時,我們要釋放內存。
~~~
SDWebImageManager *manager = [SDWebImageManager sharedManager];
// 取消正在下載的操作
[manager cancelAll];
// 清除內存緩存
[manager.imageCache clearMemory];
//釋放磁盤的緩存
[manager.imageCache cleanDisk];
~~~
上面只是實現圖片下載的方法和使用步驟而已,SDWebImage還要很多強大的功能,如本地緩存引入SDImageCache.h,SDImageCache文件里面可以看到它提供了很多方法,并可以設置緩存的時間(默認一個星期),緩存大小等等。具體用到我們看一下相應的頭文件提高的方法即可,這里不一一詳述。
cell下載圖片(SDWebImage) ?demo:
~~~
//
// AppsViewController.m
// cell圖片下載-SDWebImage
//
#import "AppsViewController.h"
#import "App.h"
#import "UIImageView+WebCache.h"
@interface AppsViewController ()
@property(nonatomic,strong) NSArray *apps;
@end
@implementation AppsViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSArray *)apps{
if (!_apps) {
NSMutableArray *appArr = [NSMutableArray array];
NSString *file = [[NSBundle mainBundle]pathForResource:@"apps" ofType:@"plist"];
NSArray *dictArr = [NSArray arrayWithContentsOfFile:file];
for (NSDictionary *dict in dictArr) {
App *app = [App appWithDict:dict];
[appArr addObject:app];
}
_apps = appArr;
}
return _apps;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.apps.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ID = @"app";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
App *app = self.apps[indexPath.row];
cell.textLabel.text = app.name;
cell.detailTextLabel.text = app.download;
//下載圖片
NSURL *url = [NSURL URLWithString:app.icon];
//[cell.imageView sd_setImageWithURL:url];
//[cell.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder"]];
//[cell.imageView sd_setImageWithURL:url completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
// NSLog(@"%@",imageURL);
//}];
//SDWebImage下載圖片
SDWebImageOptions options = SDWebImageRetryFailed | SDWebImageLowPriority;
[cell.imageView sd_setImageWithPreviousCachedImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder"] options:options progress:^(NSInteger receivedSize, NSInteger expectedSize) {
NSLog(@"下載進度:%f",(double)receivedSize/expectedSize);
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
NSLog(@"下載圖片完成%@",image);
}];
return cell;
}
@end
~~~
注意:app接收到內存警告時,要釋放內存。 整個程序的要實現警告釋放內存,因此在AppDelegate.m 的applicationDidReceiveMemoryWarning方法中釋放。
~~~
-(void)applicationDidReceiveMemoryWarning:(UIApplication *)application{
SDWebImageManager *manager = [SDWebImageManager sharedManager];
// 1.取消正在下載的操作
[manager cancelAll];
// 2.清除內存緩存
[manager.imageCache clearMemory];
}
~~~
- 前言
- iOS開發實踐之SQLite3
- iOS開發實踐之FMDB
- Obj-C與javascript交互之WebViewJavascriptBridge
- iOS開發實踐之UIWebView
- iOS開發實踐之多線程(基本概念)
- iOS開發實踐之多線程(NSThread)
- iOS開發實踐之多線程(GCD)
- iOS開發實踐之多線程(單例模式)
- iOS開發實踐之xib加載注意問題
- iOS開發實踐之多線程(NSOperation)
- iOS開發實踐之cell下載圖片(NSOperation)
- iOS開發實踐之cell下載圖片(自定義NSOperation)
- iOS開發實踐之cell下載圖片(SDWebImage)
- iOS開發實踐之JSON
- iOS開發實踐之XML
- iOS開發實踐之GET和POST請求
- iOS開發實踐之網絡檢測Reachability
- iOS開發實踐之MD5加密