您的位置:首页 > 移动开发 > IOS开发

开源框架SDWebImage的基本实现及思想

2016-01-08 23:52 489 查看

一、SDWebImage的基本介绍

SDWebImage框架是一个强大的异步加载网络图片的iOS框架。接口丰富,使用方便简单。主要支持的功能有:

图片的异步加载。

异步自动处理过期缓存和磁盘缓存。

GIF和WebP的格式支持。

图片的下载进度等等。

对同一个URL,同一个Image短时间内多次请求加载不同URL,假的URL的请求过滤。不会被重复加载。

作者的详细介绍及使用说明参见GitHub。

托管地址:https://github.com/rs/SDWebImage

下载地址:https://codeload.github.com/rs/SDWebImage/zip/master

二、SDWebImage的基本使用

首先,从GitHub上下载Zip文件,解压后有示例程序和框架文件。将桌面上的\User\Desktop\SDWebImage\SDWebImage文件夹拖拽到工程内。并在需要从网络加载图片的文件内UIImageView+WebCache.h文件。

然后,即可通过UIImageView对象调用方法,传入URL等参数从网络上加载图片。常用的方法如下:

/// 图片缓存
[self.image sd_setImageWithURL:imageURL];

/// 图片缓存,增加图片加载结束后的回调函数参数
[self.image sd_setImageWithURL:imageURL completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
NSLog(@"图片加载完成后的回调函数");
}];

/// 图片缓存,带占位图
[self.image sd_setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"default"]];

/// 图片缓存,带占位图和图片加载结束后的回调函数参数
[self.image sd_setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"default"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
NSLog(@"图片加载完成后做的事情");
}];

//options 选择方式
[self.image1 sd_setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"default"] options:SDWebImageRetryFailed];
/*
失败后重试
SDWebImageRetryFailed = 1 << 0,

//UI交互期间开始下载,导致延迟下载比如UIScrollView减速。
SDWebImageLowPriority = 1 << 1,

//只进行内存缓存
SDWebImageCacheMemoryOnly = 1 << 2,

//这个标志可以渐进式下载,显示的图像是逐步在下载
SDWebImageProgressiveDownload = 1 << 3,

//刷新缓存
SDWebImageRefreshCached = 1 << 4,

//后台下载
SDWebImageContinueInBackground = 1 << 5,

//NSMutableURLRequest.HTTPShouldHandleCookies = YES;

SDWebImageHandleCookies = 1 << 6,

//允许使用无效的SSL证书
//SDWebImageAllowInvalidSSLCertificates = 1 << 7,

//优先下载
SDWebImageHighPriority = 1 << 8,

//延迟占位符
SDWebImageDelayPlaceholder = 1 << 9,

//改变动画形象
SDWebImageTransformAnimatedImage = 1 << 10,
*/


三、SDWebImage的简单实现

//
//  DownloaderOperation.m
//

#import "DownloaderOperation.h"

@interface DownloaderOperation ()

/// 接收图片URL
@property (nonatomic, copy) NSString *URLString;
/// 下载完成后调用的回调函数
@property (nonatomic, copy) void (^finishedBlock)(UIImage *image);

@end

@implementation DownloaderOperation

/**
*初始化的类方法
*参数1:图片的URL
*参数2:加载在完成后的回调函数
*/
+ (instancetype)downloaderOperationWithURLString:(NSString *)URLString finishedBlock:(void (^)(UIImage *))finishedBlock{
DownloaderOperation *op = [[DownloaderOperation alloc] init];
op.URLString = URLString;
op.finishedBlock = finishedBlock;

return op;
}

// 自定义操作的执行的入口,操作就会自动的执行该方法。
- (void)main{
NSLog(@"传入 %@",[self.URLString lastPathComponent]);

// 模拟网络延迟
[NSThread sleepForTimeInterval:1.0];

// 实现下载
NSURL *url = [NSURL URLWithString:self.URLString];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *imgae = [UIImage imageWithData:data];

// 在耗时操作后进行取消判断
if (self.isCancelled) {
NSLog(@"取消 %@",[self.URLString lastPathComponent]);
return;
}

NSAssert(self.finishedBlock != nil, @"下载完成的回调 self.finishedBlock 不能为空");

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"下载 %@",[self.URLString lastPathComponent]);
self.finishedBlock(imgae);
}];
}

@end


//
//  ViewController.m
//

#import "ViewController.h"
#import "AppInfo.h"
#import "DownloaderOp
4000
eration.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
/// 模型数组
@property (nonatomic, strong) NSArray *modelArr;
/// 操作缓存池
@property (nonatomic, strong) NSMutableDictionary *operationCache;
/// 保存上一次的图片地址
@property (nonatomic, copy) NSString *lastURL;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self downloadImage];
}

- (void)downloadImage
{
// 随机从模型数组中取出模型,在取出地址
// 创建随机数
int random = arc4random_uniform((u_int32_t)self.modelArr.count);

// 随机的取模型
AppInfo *app = self.modelArr[random];

// 一定要在保存之前判断
if (![app.icon isEqualToString:self.lastURL]) {
// 取出上一次的操作
[[self.operationCache objectForKey:self.lastURL] cancel];
} else {
return;
}

// 保存图片地址
self.lastURL = app.icon;

// 建立下载操作.// 从模型去地址
DownloaderOperation *op = [DownloaderOperation downloaderOperationWithURLString:app.icon finishedBlock:^(UIImage *image) {
self.iconImageView.image = image;
}];
// 保存下载操作
[self.operationCache setObject:op forKey:app.icon];
// 将操作添加到队列
[self.queue addOperation:op];
}

// 懒加载
- (NSArray *)modelArr
{
if (_modelArr == nil) {
_modelArr = [AppInfo appInfos];
}
return _modelArr;
}

- (NSMutableDictionary *)operationCache
{
if (_operationCache == nil) {
_operationCache = [[NSMutableDictionary alloc] init];
}
return _operationCache;
}

@end


四、SDWebImage的开发思想

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息