前兩篇是自己寫的下載圖片方法,現在用第三方框架只要幾行代碼就可以實現圖片的下載。SDWebImage底層實現的思路也是和前面說的一樣。 SDWebImage是網絡圖片處理框架,封裝很很多方法,例如:圖片下載、圖片緩存、下載進度監聽、gif處理等等。大大提高了網絡圖片處理的效率。值得使用。
github托管地址:https://github.com/rs/SDWebImage
實現cell圖片下載之前,先了解一下SDWebImage的使用:
1、導入框架,引入頭文件:
#import "UIImageView+WebCache.h"
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
SDWebImageManager *manager = [SDWebImageManager sharedManager]; // 取消正在下載的操作 [manager cancelAll]; // 清除內存緩存 [manager.imageCache clearMemory]; //釋放磁盤的緩存 [manager.imageCache cleanDisk];
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]; }