*溫馨提示: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] <UIImage: 0x7fe8a3f0d900>, {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] <UIImage: 0x7fe8a3f0d900>, {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 網絡