*溫馨提示:SDWebImage-master可以在github上下載半沽。*
當(dāng)需要應(yīng)用SDWeb時(shí)把文件夾里的SDWebImage文件夾放入工程里。
在需要使用網(wǎng)絡(luò)獲取圖片的文件里進(jìn)入頭文件#import "UIImageView+WebCache.h"即可使用相應(yīng)的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
在很多情況下由于網(wǎng)絡(luò)的關(guān)系,圖片無(wú)法下載吨述,這是會(huì)存在留白現(xiàn)象具伍,這將會(huì)是一種很不好的app體驗(yàn),因此纵散,為了解決這一弊端梳码,此三方還有占位圖片(在下載圖片的時(shí)候,此圖片心事出來(lái)伍掀。)
代碼:
#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
依舊是有些時(shí)候(我也不知道為什么會(huì)有這么多有些時(shí)候)下載完圖片需要某些操作掰茶,因此block是一種很不錯(cuò)的選擇。
代碼
#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];
}
/*
結(jié)果
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
再接著...
很多時(shí)候我們查看圖片硕盹,也就是下載圖片的時(shí)候符匾,圖片上有??圈圈不停的轉(zhuǎn),這個(gè)很好理解瘩例,就是簡(jiǎn)單的風(fēng)火輪啊胶。
但是還有一種現(xiàn)象就是下載的進(jìn)度。下載進(jìn)度是怎么實(shí)現(xiàn)的的垛贤。
下面看代碼里是 如何實(shí)現(xiàn)的吧焰坪。
代碼:
#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所有選項(xiàng):
//失敗后重試 (經(jīng)常使用)
SDWebImageRetryFailed = 1 << 0,
//UI交互期間開始下載,導(dǎo)致延遲下載比如UIScrollView減速聘惦。(經(jīng)常使用)
SDWebImageLowPriority = 1 << 1,
//只進(jìn)行內(nèi)存緩存
SDWebImageCacheMemoryOnly = 1 << 2,
//這個(gè)標(biāo)志可以漸進(jìn)式下載,顯示的圖像是逐步在下載
SDWebImageProgressiveDownload = 1 << 3,
//刷新緩存
SDWebImageRefreshCached = 1 << 4,
//后臺(tái)下載
SDWebImageContinueInBackground = 1 << 5,
//優(yōu)先下載
SDWebImageHighPriority = 1 << 8,
//延遲占位符
SDWebImageDelayPlaceholder = 1 << 9,
//改變動(dòng)畫形象
SDWebImageTransformAnimatedImage = 1 << 10,
*/
[imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"這里是占位圖片"] options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {
// receivedSize 已經(jīng)下載大小
// expectedSize 原大小
// 動(dòng)動(dòng)我們聰明的腦袋某饰,該怎么做呢?善绎?
// 在這里我就不實(shí)現(xiàn)了黔漂,大家可以多聯(lián)系聯(lián)系。
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
NSLog(@"下載完成");
}];
[self.view addSubview:imageView];
}
/*
結(jié)果
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 網(wǎng)絡(luò)