Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
图片下载和缓存封装 Image cache and download
Branch: master
Pull request Compare This branch is 5 commits behind JimLiu:master.

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
ASIHTTPRequest
ImageDownloader
Sample
README

README

前提条件
1. 需要依赖于ASIHttpRequest


使用说明
参考AppCell类

1.头文件
#import "ImageDownloader.h"
#import "ImageDownloadReceiver.h"

2. 定义全局变量
    UIImage *_image; // 存放下载图片的变量
    ImageDownloadReceiver *_imageDownloadReceiver; //图片下载接受器
    
3. 构造函数中实例化接收器
	_imageDownloadReceiver = [[ImageDownloadReceiver alloc]initWithContainer:self];    
    
4. 对应的析构函数

- (void)dealloc
{
    _imageDownloadReceiver.imageContainer = nil;
    [_imageDownloadReceiver release];
    [_image release];
    [super dealloc];
}

5. 下载图片
	// 插到下载队列最前面优先下载当前图片
	[[ImageDownloader photosDownloader] activeRequest:IMAGEURLString delegate:_imageDownloadReceiver];

	// 插到下载队列最后面
	[[ImageDownloader photosDownloader] queueImage:IMAGEURLString delegate:_imageDownloadReceiver];
	
	// 解除下载
	[[ImageDownloader photosDownloader] removeDelegate:_imageDownloadReceiver forURL:IMAGEURLString];

6. 图片下载成功失败处理方法

- (void)imageDidDownload:(NSData *)imageData url:(NSString *)url {
    NSLog(@"imageDidDownload: %@", url);
    UIImage *image = [UIImage imageWithData:imageData];
    if (image) {
        [_image release];
        _image = [image retain];
        [self setNeedsDisplay];
        [self setNeedsLayout];
    }
    
}


- (void)imageDownloadFailed:(NSError *)error url:(NSString *)url {
    NSLog(@"imageDownloadFailed: %@, %@", url, [error localizedDescription]);
}
Something went wrong with that request. Please try again.