2.4版本2011年10月1日发布
1. 类结构新增了两个类
- UIButton+WebCache 设置Button Image
//------------------------------2.4版本更新-功能扩展-------------------------
@interface UIButton (WebCache) <SDWebImageManagerDelegate>
- (void)setImageWithURL:(NSURL *)url;
- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder;
- (void)cancelCurrentImageLoad;
@end
//-----------------------------------end-----------------------------------
//------------------------------2.4版本更新-功能优化-------------------------
@implementation UIButton (WebCache)
- (void)setImageWithURL:(NSURL *)url{
[self setImageWithURL:url placeholderImage:nil];
}
- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder{
SDWebImageManager *manager = [SDWebImageManager sharedManager];
//从队列中删除正在进行中的下载加载程序
[manager cancelForDelegate:self];
[self setImage:placeholder forState:UIControlStateNormal];
if (url){
[manager downloadWithURL:url delegate:self];
}
}
- (void)cancelCurrentImageLoad{
[[SDWebImageManager sharedManager] cancelForDelegate:self];
}
- (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image{
//下载成功
//正常情况下Button图片(默认情况下):UIControlStateNormal
[self setImage:image forState:UIControlStateNormal];
}
@end
//-----------------------------------end-----------------------------------
- SDWebImageCompat.h 系统版本兼容(设备、平台兼容)
#import <TargetConditionals.h>
//-------------------2.4版本更新-新增平台兼容-------------------
//TARGET_OS_IPHONE //真机
//TARGET_IPHONE_SIMULATOR //模拟器
#if !TARGET_OS_IPHONE
#import <AppKit/AppKit.h>
#ifndef UIImage
#define UIImage NSImage
#endif
#ifndef UIImageView
#define UIImageView NSImageView
#endif
#else
#import <UIKit/UIKit.h>
#endif
//----------------------------end----------------------------
2. SDWebImageManager
- 重命名下载器代理对象集合(
downloadDelegates
)
- 新增缓存器代理对象集合
- 新增重新下载功能(下载失败了)
@interface SDWebImageManager : NSObject <SDWebImageDownloaderDelegate, SDImageCacheDelegate>{
NSMutableArray *downloaders;
NSMutableDictionary *downloaderForURL;
NSMutableArray *failedURLs;
//-----------------------2.4版本更新-下载回调与缓存回调-----------------------
//存储下载器代理对象(下载多张图片,存在多个代理)
NSMutableArray *downloadDelegates;
//存储缓存器代理对象(缓存多张图片,存在多个代理)
NSMutableArray *cacheDelegates;
//------------------------------------end---------------------------------
}
//-----------------------2.4版本更新-新增-重新下载方法 、低优先级回调-----------------------
- (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)delegate retryFailed:(BOOL)retryFailed;
- (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)delegate retryFailed:(BOOL)retryFailed lowPriority:(BOOL)lowPriority;
//------------------------------------end---------------------------------
@end
3. SDWebImageManagerDelegate
@protocol SDWebImageManagerDelegate <NSObject>
@optional
- (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image;
//-----------------------2.4版本更新-错误回调-----------------------
- (void)webImageManager:(SDWebImageManager *)imageManager didFailWithError:(NSError *)error;
//---------------------------------end------------------------------
@end
4. SDWebImageDownloader
- 新增下载器通知(通知的方式回调)
开始下载:SDWebImageDownloadStartNotification
结束下载:SDWebImageDownloadStopNotification
- 增加用户信息属性与下载优先级
用户信息属性:userInfo(保存一些公共参数信息)
下载优先级:lowPriority(是否重新下载)
- 新增两个方法重载
用户信息:
+ (id)downloaderWithURL:(NSURL *)url delegate:(id<SDWebImageDownloaderDelegate>)delegate userInfo:(id)userInfo;
下载优先级:
+ (id)downloaderWithURL:(NSURL *)url delegate:(id<SDWebImageDownloaderDelegate>)delegate userInfo:(id)userInfo lowPriority:(BOOL)lowPriority;
//-----------------------2.4版本更新-功能优化-----------------------
extern NSString *const SDWebImageDownloadStartNotification;
extern NSString *const SDWebImageDownloadStopNotification;
//-----------------------------end-------------------------------
@interface SDWebImageDownloader : NSObject{
@private
NSURL *url;
NSURLConnection *connection;
NSMutableData *imageData;
//-----------------------2.4版本更新-功能优化-----------------------
id userInfo;
BOOL lowPriority;
//-----------------------------end-------------------------------
}
@property (nonatomic, retain) NSURL *url;
@property (nonatomic, assign) id<SDWebImageDownloaderDelegate> delegate;
@property (nonatomic, retain) NSMutableData *imageData;
//-----------------------2.4版本更新-功能优化--------------
@property (nonatomic, retain) id userInfo;
@property (nonatomic, readwrite) BOOL lowPriority;
//-----------------------------end----------------------
+ (id)downloaderWithURL:(NSURL *)url delegate:(id<SDWebImageDownloaderDelegate>)delegate;
//-----------------------2.4版本更新-新增功能--------------
+ (id)downloaderWithURL:(NSURL *)url delegate:(id<SDWebImageDownloaderDelegate>)delegate userInfo:(id)userInfo lowPriority:(BOOL)lowPriority;
+ (id)downloaderWithURL:(NSURL *)url delegate:(id<SDWebImageDownloaderDelegate>)delegate userInfo:(id)userInfo;
//-----------------------------end----------------------
@end
//-----------------------2.4版本更新-功能优化-----------------------
NSString *const SDWebImageDownloadStartNotification = @"SDWebImageDownloadStartNotification";
NSString *const SDWebImageDownloadStopNotification = @"SDWebImageDownloadStopNotification";
//-----------------------------end-------------------------------
@implementation SDWebImageDownloader
//-----------------------------------2.4版本更新-新增功能----------------------------
+ (id)downloaderWithURL:(NSURL *)url delegate:(id<SDWebImageDownloaderDelegate>)delegate{
return [self downloaderWithURL:url delegate:delegate userInfo:nil];
}
+ (id)downloaderWithURL:(NSURL *)url delegate:(id<SDWebImageDownloaderDelegate>)delegate userInfo:(id)userInfo{
return [self downloaderWithURL:url delegate:delegate userInfo:userInfo lowPriority:NO];
}
+ (id)downloaderWithURL:(NSURL *)url delegate:(id<SDWebImageDownloaderDelegate>)delegate userInfo:(id)userInfo lowPriority:(BOOL)lowPriority{
//绑定SDNetworkActivityIndicator(下载地址:http://github.com/rs/SDNetworkActivityIndicator)
//要使用它,除了SDWebImage的导入,只需添加#import "SDNetworkActivityIndicator
if (NSClassFromString(@"SDNetworkActivityIndicator")){
id activityIndicator = [NSClassFromString(@"SDNetworkActivityIndicator") performSelector:NSSelectorFromString(@"sharedActivityIndicator")];
[[NSNotificationCenter defaultCenter] addObserver:activityIndicator
selector:NSSelectorFromString(@"startActivity")
name:SDWebImageDownloadStartNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:activityIndicator
selector:NSSelectorFromString(@"stopActivity")
name:SDWebImageDownloadStopNotification object:nil];
}
SDWebImageDownloader *downloader = [[SDWebImageDownloader alloc] init];
downloader.url = url;
downloader.delegate = delegate;
downloader.userInfo = userInfo;
downloader.lowPriority = lowPriority;
[downloader performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:YES];
return downloader;
}
//-----------------------------------------end------------------------------------
- (void)start{
//------------------------------2.4版本更新-功能优化-------------------------
//为了防止潜在的重复缓存(NSURLCache + SDImageCache),我们为图像请求禁用缓存
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:15];
self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
//如果不是低优先级模式,确保我们不会被UI操作阻塞(NSURLConnection的默认runloop模式是NSEventTrackingRunLoopMode)
if (!lowPriority){
[connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}
[connection start];
if (connection){
self.imageData = [NSMutableData data];
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStartNotification object:nil];
} else {
if ([delegate respondsToSelector:@selector(imageDownloader:didFailWithError:)]){
[delegate performSelector:@selector(imageDownloader:didFailWithError:) withObject:self withObject:nil];
}
}
//--------------------------------------end-------------------------------
}
//------------------------------2.4版本更新-功能优化-------------------------
#pragma GCC diagnostic ignored "-Wundeclared-selector"
//-----------------------------------end-----------------------------------
- (void)connectionDidFinishLoading:(NSURLConnection *)aConnection{
self.connection = nil;
//------------------------------2.4版本更新-功能优化-------------------------
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
//-----------------------------------end-----------------------------------
if ([delegate respondsToSelector:@selector(imageDownloaderDidFinish:)]){
[delegate performSelector:@selector(imageDownloaderDidFinish:) withObject:self];
}
if ([delegate respondsToSelector:@selector(imageDownloader:didFinishWithImage:)]){
UIImage *image = [[UIImage alloc] initWithData:imageData];
[delegate performSelector:@selector(imageDownloader:didFinishWithImage:) withObject:self withObject:image];
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
//------------------------------2.4版本更新-功能优化-------------------------
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
//-----------------------------------end-----------------------------------
if ([delegate respondsToSelector:@selector(imageDownloader:didFailWithError:)]){
[delegate performSelector:@selector(imageDownloader:didFailWithError:) withObject:self withObject:error];
}
self.connection = nil;
self.imageData = nil;
}
//------------------------------2.4版本更新-功能优化-------------------------
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
//-----------------------------------end----------------------------------
@end
5. SDImageCache
- 新增系统平台兼容iPhone系统
TARGET_OS_IPHONE
//-----------------------2.4版本更新-系统版本兼容-----------------------
#if TARGET_OS_IPHONE
//--------------------------------end--------------------------------
//订阅应用程序事件
//应用程序终止,里面回调didReceiveMemoryWarning方法,清空内存
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(clearMemory)
name:UIApplicationDidReceiveMemoryWarningNotification
object:nil];
//应用程序终止,里面回调willTerminate方法,清空磁盘
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(cleanDisk)
name:UIApplicationWillTerminateNotification
object:nil];
//-----------------------2.1版本更新-系统版本兼容-----------------------
#ifdef __IPHONE_4_0
UIDevice *device = [UIDevice currentDevice];
if ([device respondsToSelector:@selector(isMultitaskingSupported)] && device.multitaskingSupported){
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(clearMemory)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
}
#endif
//--------------------------------end--------------------------------
#endif
- 将图片数据缓存磁盘支持了
TARGET_OS_IPHONE
- (void)storeKeyWithDataToDisk:(NSArray *)keyAndData{
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSString *key = [keyAndData objectAtIndex:0];
NSData *data = [keyAndData count] > 1 ? [keyAndData objectAtIndex:1] : nil;
if (data){
[fileManager createFileAtPath:[self cachePathForKey:key] contents:data attributes:nil];
} else {
//如果没有给定的数据表示,转换JPEG中的UIImage并存储它
//这个技巧占用更多的CPU/内存,并且不保留alpha通道
//线程安全,没有锁
UIImage *image = [self imageFromKey:key fromDisk:YES];
if (image){
//-----------------------2.4版本更新-系统版本兼容-----------------------
#if TARGET_OS_IPHONE
[fileManager createFileAtPath:[self cachePathForKey:key] contents:UIImageJPEGRepresentation(image, (CGFloat)1.0) attributes:nil];
#else
NSArray* representations = [image representations];
NSData* jpegData = [NSBitmapImageRep representationOfImageRepsInArray: representations usingType: NSJPEGFileType properties:nil];
[fileManager createFileAtPath:[self cachePathForKey:key] contents:jpegData attributes:nil];
#endif
//--------------------------------end--------------------------------
}
}
}
- (void)storeImage:(UIImage *)image imageData:(NSData *)data forKey:(NSString *)key toDisk:(BOOL)toDisk{
//-----------------------2.4版本更新-功能优化-----------------------
if (!image || !key){
return;
}
[memCache setObject:image forKey:key];
if (toDisk){
if (!data)
return;
NSArray *keyWithData;
if (data){
keyWithData = [NSArray arrayWithObjects:key, data, nil];
}else{
keyWithData = [NSArray arrayWithObjects:key, nil];
}
[cacheInQueue addOperation:[[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(storeKeyWithDataToDisk:) object:keyWithData]];
}
//------------------------------end------------------------------
}
网友评论