SDWebImage图片下载,内存警告处理
//
// ViewController.m
// DownLoadImage
//
// Created by fe on 2016/10/21.
// Copyright © 2016年 fe. All rights reserved.
//
#import "ViewController.h"
#import "DownloadModel.h"
#import "UIImageView+WebCache.h"
@interface ViewController ()
@property (nonatomic,strong) NSArray *sourceArray;//数据源数组
@property (nonatomic,strong) NSMutableDictionary *bufferMemonry;//图片内存缓存
@property (nonatomic,strong) NSOperationQueue *operationQueue;//下载图片的队列
@property (nonatomic,strong) NSMutableDictionary *operationMemonry;//操作缓存,避免重复添加操作
@end
@implementation ViewController
- (NSArray *)sourceArray
{
if (!_sourceArray) {
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"SourceList.plist" ofType:nil];
NSArray *dicArray = [NSArray arrayWithContentsOfFile:plistPath];
NSMutableArray *objectArray = [NSMutableArray array];
for (NSDictionary *dic in dicArray) {
DownloadModel *sourceModel = [DownloadModel downloadModelWithDic:dic];
[objectArray addObject:sourceModel];
}
_sourceArray = objectArray;
}
return _sourceArray;
}
- (NSOperationQueue *)operationQueue
{
if (!_operationQueue) {
_operationQueue = [[NSOperationQueue alloc] init];
_operationQueue.maxConcurrentOperationCount = 5;
}
return _operationQueue;
}
//懒加载方式创建内存缓存字典
- (NSMutableDictionary *)bufferMemonry
{
if (!_bufferMemonry) {
_bufferMemonry = [[NSMutableDictionary alloc] init];
}
return _bufferMemonry;
}
- (NSMutableDictionary *)operationMemonry
{
if (!_operationMemonry) {
_operationMemonry = [[NSMutableDictionary alloc] init];
}
return _operationMemonry;
}
- (void)viewDidLoad {
[super viewDidLoad];
//单纯下载一张图片
/*
第一个参数:要下载图片的地址
第二个参数:占位图片
第三个参数:下载图片的策略
第四个参数:progress是一个block,监听下载进度
receiveSize:已经下载的大小
expectedSize:图片大小
第五个参数:下载完成后调用
*/
//[[SDWebImageManager sharedManager] downloadImageWithURL:<#(NSURL *)#> options:<#(SDWebImageOptions)#> progress:<#^(NSInteger receivedSize, NSInteger expectedSize)progressBlock#> completed:<#^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL)completedBlock#>]
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.sourceArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *reuseIdentifier = @"downloadImage";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
DownloadModel *sourceModel = self.sourceArray[indexPath.row];
/*
第一个参数:要下载图片的地址
第二个参数:占位图片
第三个参数:下载图片的策略
第四个参数:progress是一个block,监听下载进度
receiveSize:已经下载的大小
expectedSize:图片大小
第五个参数:下载完成后调用
*/
NSURL *url = [NSURL URLWithString:sourceModel.icon];
[cell.imageView sd_setImageWithURL:url placeholderImage:nil options:kNilOptions progress:^(NSInteger receivedSize, NSInteger expectedSize) {
NSLog(@"-------%.2f",1.0 *receivedSize/expectedSize);
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
}];
cell.textLabel.text = sourceModel.title;
cell.detailTextLabel.text = sourceModel.subTitle;
return cell;
}
//当下载的图片较多接收到内存警告的时候,清空缓存和队列任务
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
self.bufferMemonry = nil;
self.operationMemonry = nil;
[self.operationQueue cancelAllOperations];
}
@end
应用级别的内存警告处理
//
// AppDelegate.m
// DownLoadImage
//
// Created by fe on 2016/10/21.
// Copyright © 2016年 fe. All rights reserved.
//
#import "AppDelegate.h"
#import "SDWebImageManager.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
return YES;
}
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
//1:取消所有当前的任务
[[SDWebImageManager sharedManager] cancelAll];
//2:清空缓存
//缓存周期为7天,
//cleanDisk:清除过期缓存,计算缓存文件大小,如果缓存文件大小超过设定值,会继续清除,按缓存文件时间的远近来删除
//clearDisk:删除文件夹,重新创建
//[[SDWebImageManager sharedManager].imageCache cleanDisk];
//[[SDWebImageManager sharedManager].imageCache clearDisk];
}
@end