猫猫学iOS 之微博项目实战(8)用AFNetworking和SDWebImage简单加载微博数据
来源:互联网 发布:淘宝助理多店共享面单 编辑:程序博客网 时间:2024/05/15 04:24
猫猫分享,必须精品
原创文章,欢迎转载。转载请注明:翟乃玉的博客
地址:http://blog.csdn.net/u013357243
一:效果
没有图文混排,也没有复杂的UI,仅仅是简单的显示出微博数据,主要介绍AFNetworking和SDWebImage的简单用法
二:加载数据AFNetworking
AFNetworking用法
AFNetworking的用法大体有三步:
一:下载第三方框架(githup也好,百度也好,多的是)
二:导入头文件 #import "AFNetworking.h"
三:开始写代码(以上两步所有的第三方框架都通用)
1,请求管理者
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
2,拼接请求参数
NSMutableDictionary *params = [NSMutableDictionary dictionary]; params[@"*****"] = @"*****"; params[@"*****"] = @"*****"; params[@"*****"] = @"*****"; 。。。。 ///可以写很多参数
3,发送请求
[mgr GET:@"https:请求的网址" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { //这里写请求成功用的代码 NSLog(@"请求成功 --- %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { //这里写请求失败用的代码 NSLog(@"请求失败 --- %@",error); }];
下面贴出微博项目中的代码
加载最新的微博数据
/** * //加载最新的微博数据 * * */-(void)loadNewStatus{ //1,请求管理者 AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager]; //2,拼接请求参数 NSMutableDictionary *params = [NSMutableDictionary dictionary]; HWAccount *account = [HWAccountTool account]; params[@"access_token"] = account.access_token;// params[@"count"] = @20; //3,发送请求 [mgr GET:@"https://api.weibo.com/2/statuses/friends_timeline.json" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { HWLog(@"请求成功 --- %@", responseObject); //取得微博数组 self.statuses = responseObject[@"statuses"]; //刷新表格 [self.tableView reloadData]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { HWLog(@"请求失败 --- %@",error); }];}
三:SDWebImage用法
SDWebImage的用法大体有三步:
一:下载第三方框架(githup也好,百度也好,多的是)
二:导入头文件 #import "UIImageView+WebCache.h" (这里面很多头文件,看自己具体需要那种了)
三:开始写代码(以上两步所有的第三方框架都通用)
这个代码写起来就更简单了,例如微博中我们想要让他自己下载缓存一张图片用作每个tableViewCell的图片,并且显示一张占位图片,一句代码就搞定了
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:placehoder];
然后需要做防止程序内存溢出的操作
一:在程序AppDelegate 中写入头文件#import "SDWebImageManager.h"
二:调用方法,在整个程序内存警报时候调用-(void)applicationDidReceiveMemoryWarning:(UIApplication *)application
三:方法内写入
//整个程序内存警报时候调用-(void)applicationDidReceiveMemoryWarning:(UIApplication *)application{ SDWebImageManager *mgr = [SDWebImageManager sharedManager]; //1,取消下载 [mgr cancelAll]; //2,清除内存中的所有图片 [mgr.imageCache clearMemory];}
这里贴出cell全部代码供参考
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *ID = @"status"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; } //用indexPathRow取出对应的一条微博字典 NSDictionary *status = self.statuses[indexPath.row]; //设置微博作者 NSDictionary *user = status[@"user"]; cell.textLabel.text = user[@"name"]; //设置微博内容 cell.detailTextLabel.text = status[@"text"]; //设置微博头像 NSString *imageUrl = user[@"profile_image_url"]; //占位图 UIImage *placehoder = [UIImage imageNamed:@"avatar_default_small"]; [cell.imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:placehoder]; return cell;}
11 0
- 猫猫学iOS 之微博项目实战(8)用AFNetworking和SDWebImage简单加载微博数据
- AJ学IOS 之微博项目实战(8)用AFNetworking和SDWebImage简单加载微博数据
- 猫猫学iOS 之微博项目实战(7)程序启动新特性用UICollectionViewController实现
- SDWebImage和AFNetWorking
- 猫猫学iOS 之微博项目实战(4)微博自定义tabBar中间的添加按钮
- 猫猫学iOS 之微博项目实战(5)微博自定义搜索框searchBar
- 猫猫学iOS 之微博项目实战(1)微博主框架-子控制器的添加
- 猫猫学iOS 之微博项目实战(1)微博主框架-子控制器的添加
- 猫猫学iOS 之微博项目实战(2)微博主框架-自定义导航控制器NavigationController
- AJ学IOS 之微博项目实战(7)程序启动新特性用UICollectionViewController实现
- iOS开发网络数据之AFNetworking使用2.0和2.5
- 使用AFNetworking, SDWebimage和OHHTTPStubs
- 使用AFNetworking, SDWebimage和OHHTTPStubs
- 使用AFNetworking, SDWebimage和OHHTTPStubs
- AFNetworking和SDWebImage实现原理
- 使用AFNetworking, SDWebimage和OHHTTPStubs
- iOS基于SDWebimage和urlConnection简单的带进度条图片加载
- iOS 数据请求类AFNetworking 简单封装
- python遇到‘\u’开头的unicode编码
- ios 获取设备的ip地址
- 数理逻辑资源
- pdf转换成html具体怎么转
- STL中的TBits使用
- 猫猫学iOS 之微博项目实战(8)用AFNetworking和SDWebImage简单加载微博数据
- OpenCV入门笔记(五) 轮廓检测
- 【从头开始写操作系统系列】使用 Bochs 调试操作系统
- Understanding Java Garbage Collection
- 金刚石图纹
- Spark入门三部曲之第三步Spark程序的开发和运行及WordCount
- Python Requests快速入门
- Mysql数据库导出导入
- 2014年下半年阅读书单