SDWebImage图片处理框架

SDWebImage框架

图片处理框架 包含的功能:图片下载、图片缓存、下载进度监听、gif处理等等 用法极其简单,功能十分强大,大大提高了网络图片的处理效率 国内超过90%的iOS项目都有它的影子

SDWebImage常用方法- (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;SDWebImage内存处理

内存处理:当app接收到内存警告时调用

– (void)applicationDidReceiveMemoryWarning:(UIApplication *)application{SDWebImageManager *mgr = [SDWebImageManager sharedManager];// 1.取消正在下载的操作[mgr cancelAll];// 2.清除内存缓存[mgr.imageCache clearMemory];}SDWebImageOptionsSDWebImageRetryFailed : //下载失败后,会自动重新下载SDWebImageLowPriority : //当正在进行UI交互时,自动暂停内部的一些下载操作SDWebImageRetryFailed | SDWebImageLowPriority : //拥有上面2个功能SDWebImage图片缓存流程分析

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]; SDWebImage实例

使用blocks,,你将被告知下载进度,完成时是成功还是失败:

@interface AppsViewController ()/** * 所有的应用数据 */@property (nonatomic, strong) NSMutableArray *apps;#pragma mark – 懒加载- (NSMutableArray *)apps{if (!_apps) {// 1.加载plistNSString *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];}= 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{;}- (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摘抄美文4、承诺是一件美好的事情,但美好的东西往往不会变为现实。

SDWebImage图片处理框架

相关文章:

你感兴趣的文章:

标签云: