SDWebImage
2017-03-24 本文已影响11人
PZcoder
SDWebImage使用简介
SDWebImage是一个缓存图片的第三方,下面介绍几种简单使用
GitHub下载地址:https://github.com/rs/SDWebImage
NSLog(@"%@",[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject]);
//打印沙盒路径,缓存图片保存在沙盒路径中。
#pragma mark 下载图片
- (IBAction)tapDownLoadPic:(UIButton *)sender {
NSString *urlStr1 = @"http://img02.tooopen.com/images/20160327/tooopen_sy_157439572224.jpg";
NSString *urlStr2 = @"http://img02.tooopen.com/images/20160320/tooopen_sy_156512393637.jpg";
NSURL *url1 = [NSURL URLWithString:urlStr1];
NSURL *url2 = [NSURL URLWithString:urlStr2];
//直接下载缓存图片
[self.image1 sd_setImageWithURL:url1];
//带有占位图片的,缓存结束后自动替换
[self.image2 sd_setImageWithURL:url2 placeholderImage:[UIImage imageNamed:@"111"]];
//block中可以查看图片来源
[self.image2 sd_setImageWithURL:url2 placeholderImage:[UIImage imageNamed:@"111"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
NSLog(@"url == %@",imageURL);
NSLog(@"图片来自:%ld",cacheType);
//cacheType 0- 网络 1-磁盘 2- 内存
}];
//可设置选项,SDWebImageOptions可以设置自动重试、仅保存内存中等
[self.image2 sd_setImageWithURL:url2 placeholderImage:[UIImage imageNamed:@"111"] options:SDWebImageRetryFailed];
//监测下载进度
[self.image2 sd_setImageWithURL:url2 placeholderImage:[UIImage imageNamed:@"111"] options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {
NSLog(@"进度:%lf",(double)(receivedSize/expectedSize));
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
NSLog(@"图片来自:%ld",cacheType);
}];
//观察下载进度,不显示图片。
[[SDWebImageManager sharedManager]downloadImageWithURL:url2 options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {
NSLog(@"进度:%lf",(double)(receivedSize/expectedSize));
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
NSLog(@"图片来自:%ld",cacheType);
}];
//取消下载所有
[[SDWebImageManager sharedManager] cancelAll];
}
#pragma mark 清除内存
- (IBAction)clearNeicun:(UIButton *)sender {
[[SDImageCache sharedImageCache] clearMemory];
}
#pragma mark 清除过期图片(默认7天过期)
- (IBAction)clearDateOut:(UIButton *)sender {
[[SDImageCache sharedImageCache] cleanDisk];
}
#pragma mark 清除磁盘
- (IBAction)clearDisk:(UIButton *)sender {
[[SDImageCache sharedImageCache] clearDisk];
}