标签:sdwebimage 图片处理框架 网络 下载 监听
图片处理框架
包含的功能:图片下载、图片缓存、下载进度监听、gif处理等等
用法极其简单,功能十分强大,大大提高了网络图片的处理效率
国内超过90%的iOS项目都有它的影子
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder;
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options;
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletionBlock)completedBlock;
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock;
内存处理:当app接收到内存警告时调用
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
SDWebImageManager *mgr = [SDWebImageManager sharedManager];
// 1.取消正在下载的操作
[mgr cancelAll];
// 2.清除内存缓存
[mgr.imageCache clearMemory];
}
SDWebImageRetryFailed : //下载失败后,会自动重新下载
SDWebImageLowPriority : //当正在进行UI交互时,自动暂停内部的一些下载操作
SDWebImageRetryFailed | SDWebImageLowPriority : //拥有上面2个功能
SDWebImage是一个功能很强大的缓存网络图片的框架。框架实现了异步加载网络图片、自动缓存图片数据等功能。以UIImageView
加载网络图片为例,对其总体的加载图片流程做一个大致的分析。 首先使用SDWebImage先要导入
#import "UIImageView+WebCache.h"文件
设置网络图片的图片地址就可以加载图片。
项目地址
https://github.com/rs/SDWebImage
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com/image.jpg"];
[imageView sd_setImageWithURL:url];
使用blocks,你将被告知下载进度,完成时是成功还是失败:
@interface AppsViewController ()
/**
* 所有的应用数据
*/
@property (nonatomic, strong) NSMutableArray *apps;
@end
@implementation AppsViewController
#pragma mark - 懒加载
- (NSMutableArray *)apps
{
if (!_apps) {
// 1.加载plist
NSString *file = [[NSBundle mainBundle] pathForResource:@"apps" ofType:@"plist"];
NSArray *dictArray = [NSArray arrayWithContentsOfFile:file];
// 2.字典 --> 模型
NSMutableArray *appArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
HMApp *app = [HMApp appWithDict:dict];
[appArray addObject:app];
}
// 3.赋值
self.apps = appArray;
// _apps = appArray;
}
return _apps;
}
#pragma mark - 初始化方法
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (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];
}
// 设置基本信息
cell.textLabel.text = app.name;
cell.detailTextLabel.text = app.download;
// 下载图片
NSURL *url = [NSURL URLWithString:app.icon];
UIImage *placeholder = [UIImage imageNamed:@"placeholder"];
[cell.imageView sd_setImageWithURL:url placeholderImage:placeholder];
[cell.imageView sd_setImageWithURL:url placeholderImage:placeholder completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
NSLog(@"----图片加载完毕---%@", image);
}];
SDWebImageOptions options = SDWebImageRetryFailed | SDWebImageLowPriority;
[cell.imageView sd_setImageWithURL:url placeholderImage:placeholder options:options progress:^(NSInteger receivedSize, NSInteger expectedSize) { // 这个block可能会被调用多次
NSLog(@"下载进度:%f", (double)receivedSize / expectedSize);
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
NSLog(@"----图片加载完毕---%@", image);
}];
return cell;
}
@end
内存处理:
/**
* 当app接收到内存警告
*/
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
SDWebImageManager *mgr = [SDWebImageManager sharedManager];
// 1.取消正在下载的操作
[mgr cancelAll];
// 2.清除内存缓存
[mgr.imageCache clearMemory];
}
标签:sdwebimage 图片处理框架 网络 下载 监听
原文地址:http://blog.csdn.net/wangzi11322/article/details/45332189