下载地址: https://github.com/rs/SDWebImage
1.添加文件至项目中
2.添加Framework MapKit.framework, ImageIO.framework
3.修改Build Settings
Linking中的Other Linker Flags添加 -ObjC
Search Paths中的Header Search Paths添加”$(TARGET_BUILD_DIR)/usr/local/lib/include” 和
”$(OBJROOT)/UninstalledProducts/include”
4.#import头文件UIImageView+WebCache.h
5.默认支持ARC
6.基本代码 [cell.imageView setImageWithURL:[NSURL URLWithString:[array objectAtIndex:indexPath.row]]placeholderImage:[UIImage imageNamed:@"placeholder.png"]]
7.使用SDWebImageManager类,可以进行一些异步加载的工作
SDWebImageManager*manager = [SDWebImageManager sharedManager];
NSURL *url =
[NSURL URLWithString:@"http://static2.dmcdn.net/static/video/116/367/44763611:jpeg_preview_small.jpg?20120509101749"];
UIImage *cacheImage =[[SDImageCache sharedImageCache] imageFromKey:[manager cacheKeyForURL:url]];
if (cacheImage) {
NSLog(@"done");
UIButton *btn = (UIButton *)[self.viewviewWithTag:101];
[btn setBackgroundImage:cacheImageforState:UIControlStateNormal];
}
else {
[manager downloadWithURL:url
delegate:self
options:0
success:^(UIImage *image, BOOLcached){
NSLog(@"%d",cached);
}
failure:^(NSError *error){
NSLog(@"fail");
}
];
}
-(void)webImageManager:(SDWebImageManager *)imageManagerdidFinishWithImage:(UIImage *)image
{
UIButton *btn = (UIButton *)[self.viewviewWithTag:101];
[btn setBackgroundImage:imageforState:UIControlStateNormal];
}
要实现SDWebImageManagerDelegate协议,并且要实现协议的webImageManager:didFinishWithImage:方法。
当下载完成后,调用回调方法,使下载的图片显示