SDWebImage前世今生之V2.2版本

2.2版本2010年8月29日发布

更新说明:如果图片格式是PNG或GIF,当存储到磁盘进行缓存时候,不要将图像转换为JPEG,因为他能够节省CPU和内存以及alpha通道/图像的清晰度。

1. SDWebImageDownloader 新增加了属性,在主线程下载和优化了回调处理

@property (nonatomic, retain) NSURL *url;
@property (nonatomic, assign) id delegate;
//-------------2.2版本更新-新增-新增属性-------------------
@property (nonatomic, retain) NSMutableData *imageData;
//--------------------------end-------------------------
+ (id)downloaderWithURL:(NSURL *)url delegate:(id)delegate{
    SDWebImageDownloader *downloader = [[SDWebImageDownloader alloc] init];
    downloader.url = url;
    downloader.delegate = delegate;
//-------------2.2版本更新-在主线程下载-------------------
    [downloader performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:YES];
//--------------------------end-------------------------
    return downloader;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)aConnection{
    //------------------2.2版本更新-优化----------------------
    self.connection = nil;
    //下载器下载完成
    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];
    }
    //--------------------------end--------------------------
}


2. 缓存器->SDImageCache 新增了属性和方法重载,增加了二进制NSData数据存储到磁盘

@interface SDImageCache : NSObject{
    NSMutableDictionary *memCache;
    NSString *diskCachePath;
    NSOperationQueue *cacheInQueue;
    //-------------2.2版本更新-新增-方法重载-功能抽象-------------------
    NSMutableDictionary *storeDataQueue;
    //--------------------------------end------------------------------
}

//-------------2.2版本更新-新增-方法重载-功能抽象-------------------
- (void)storeImage:(UIImage *)image imageData:(NSData *)data forKey:(NSString *)key toDisk:(BOOL)toDisk;
//-------------------------------end---------------------------
//-------------2.2版本更新-新增-方法重载-功能抽象-------------------
- (void)storeImage:(UIImage *)image forKey:(NSString *)key{
    [self storeImage:image imageData:nil forKey:key toDisk:YES];
}
- (void)storeImage:(UIImage *)image forKey:(NSString *)key toDisk:(BOOL)toDisk{
    [self storeImage:image imageData:nil forKey:key toDisk:toDisk];
}
- (void)storeImage:(UIImage *)image imageData:(NSData *)data forKey:(NSString *)key toDisk:(BOOL)toDisk{
    if (!image || !key){
        return;
    }
    if (toDisk && !data){
        return;
    }
    [memCache setObject:image forKey:key];
    if (toDisk){
        [storeDataQueue setObject:data forKey:key];
        [cacheInQueue addOperation:[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(storeKeyToDisk:) object:key]];
    }
}
//-------------------------------end---------------------------

- (void)storeKeyToDisk:(NSString *)key{
    //-------------2.2版本更新-磁盘缓存-------------------
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    NSData *data = [storeDataQueue objectForKey:key];
    if (data){
        //存储二进制->NSData
        [fileManager createFileAtPath:[self cachePathForKey:key] contents:data attributes:nil];
        @synchronized(storeDataQueue){
            [storeDataQueue removeObjectForKey:key];
        }
        //----------------------end------------------------
    } else {
        //存储图片->UIImage
        UIImage *image = [self imageFromKey:key fromDisk:YES];
        if (image){
            [fileManager createFileAtPath:[self cachePathForKey:key] contents:UIImageJPEGRepresentation(image, (CGFloat)1.0) attributes:nil];
        }
    }
}

3. SDWebImageDownloaderDelegate 新增了回调imageDownloaderDidFinish针对于imageData它而设计,后面版本迭代会更新具体实现暂时没有。

//-----------------------2.2版本更新-下载完成回调-----------------------
- (void)imageDownloaderDidFinish:(SDWebImageDownloader *)downloader;
//---------------------------------end------------------------------

你可能感兴趣的:(SDWebImage前世今生之V2.2版本)