牛骨文教育服务平台(让学习变的简单)

一:效果

没有图文混排,也没有复杂的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;
}