*温馨提示:SDWebImage-master可以在github上下载。*
当需要应用SDWeb时把文件夹里的SDWebImage文件夹放入工程里。
在需要使用网络获取图片的文件里进入头文件#import "UIImageView+WebCache.h"即可使用相应的api。
首先最基本的方法展示(仅获取图片)
代码:
#import "ViewController.h"
#import "UIImageView+WebCache.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString * urlString = [NSString stringWithFormat:@"http://localhost/tupian.jpg"];
NSURL * url = [NSURL URLWithString:urlString];
UIImageView * imageView = [[UIImageView alloc]init];
imageView.frame = CGRectMake(0, 20, CGRectGetWidth(self.view.frame), 300);
[imageView sd_setImageWithURL:url];
[self.view addSubview:imageView];
}
@end
在很多情况下由于网络的关系,图片无法下载,这是会存在留白现象,这将会是一种很不好的app体验,因此,为了解决这一弊端,此三方还有占位图片(在下载图片的时候,此图片心事出来。)
代码:
#import "ViewController.h"
#import "UIImageView+WebCache.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString * urlString = [NSString stringWithFormat:@"http://localhost/tupian.jpg"];
NSURL * url = [NSURL URLWithString:urlString];
UIImageView * imageView = [[UIImageView alloc]init];
imageView.frame = CGRectMake(0, 20, CGRectGetWidth(self.view.frame), 300);
[imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"图片名称"]];
[self.view addSubview:imageView];
}
@end
依旧是有些时候(我也不知道为什么会有这么多有些时候)下载完图片需要某些操作,因此block是一种很不错的选择。
代码
#import "ViewController.h"
#import "UIImageView+WebCache.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString * urlString = [NSString stringWithFormat:@"http://localhost/tupian.jpg"];
NSURL * url = [NSURL URLWithString:urlString];
UIImageView * imageView = [[UIImageView alloc]init];
imageView.frame = CGRectMake(0, 20, CGRectGetWidth(self.view.frame), 300);
[imageView sd_setImageWithURL:url completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
NSLog(@"%@",image);
//枚举
NSLog(@"%ld",(long)cacheType);
NSLog(@"%@",imageURL);
}];
[self.view addSubview:imageView];
}
/*
结果
2016-03-18 13:12:18.091 CX-SDWebImage-master[4626:283982] , {250, 250}
2016-03-18 13:12:18.092 CX-SDWebImage-master[4626:283982] 1
2016-03-18 13:12:18.092 CX-SDWebImage-master[4626:283982] http://localhost/tupian.jpg
*/
@end
再接着...
很多时候我们查看图片,也就是下载图片的时候,图片上有��圈圈不停的转,这个很好理解,就是简单的风火轮。
但是还有一种现象就是下载的进度。下载进度是怎么实现的的。
下面看代码里是 如何实现的吧。
代码:
#import "ViewController.h"
#import "UIImageView+WebCache.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString * urlString = [NSString stringWithFormat:@"http://localhost/tupian.jpg"];
NSURL * url = [NSURL URLWithString:urlString];
UIImageView * imageView = [[UIImageView alloc]init];
imageView.frame = CGRectMake(0, 20, CGRectGetWidth(self.view.frame), 300);
/*
options所有选项:
//失败后重试 (经常使用)
SDWebImageRetryFailed = 1 << 0,
//UI交互期间开始下载,导致延迟下载比如UIScrollView减速。(经常使用)
SDWebImageLowPriority = 1 << 1,
//只进行内存缓存
SDWebImageCacheMemoryOnly = 1 << 2,
//这个标志可以渐进式下载,显示的图像是逐步在下载
SDWebImageProgressiveDownload = 1 << 3,
//刷新缓存
SDWebImageRefreshCached = 1 << 4,
//后台下载
SDWebImageContinueInBackground = 1 << 5,
//优先下载
SDWebImageHighPriority = 1 << 8,
//延迟占位符
SDWebImageDelayPlaceholder = 1 << 9,
//改变动画形象
SDWebImageTransformAnimatedImage = 1 << 10,
*/
[imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"这里是占位图片"] options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {
// receivedSize 已经下载大小
// expectedSize 原大小
// 动动我们聪明的脑袋,该怎么做呢??
// 在这里我就不实现了,大家可以多联系联系。
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
NSLog(@"下载完成");
}];
[self.view addSubview:imageView];
}
/*
结果
2016-03-18 13:12:18.091 CX-SDWebImage-master[4626:283982] , {250, 250}
2016-03-18 13:12:18.092 CX-SDWebImage-master[4626:283982] 1
2016-03-18 13:12:18.092 CX-SDWebImage-master[4626:283982] http://localhost/tupian.jpg
*/
分类: IOS 第三方, IOS 网络