网络热恋之SDWebImage

SDWebImage-master 是一个非常强大的三方。

当需要应用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(, , CGRectGetWidth(self.view.frame), ); [imageView sd_setImageWithURL:url]; [self.view addSubview:imageView]; } @end

*温馨提示:SDWebImage-master可以在github上下载。*

防止网速差刷不出来图片,会出现留白现象,为此加了占位图片;图片不唯一可以自己配置;

 #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(, , CGRectGetWidth(self.view.frame), ); [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"图片名称"]]; [self.view addSubview:imageView]; } @end
 //
// ViewController.m
// CX-SDWebImage-master
// #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(, , CGRectGetWidth(self.view.frame), ); [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
上一篇:对cnblogs.com用户体验的评价


下一篇:Web压力测试 ApacheBench(ab)