码迷,mamicode.com
首页 > Web开发 > 详细

AFNetworking与MJExtension处理后台_图片框架SDWebImage(初稿)

时间:2015-05-24 15:47:40      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:

1.AFNetworking框架处理用户一般的POST GET等5种类型的请求

GET请求:

AFHTTPRequestOperationManager *mgr=[AFHTTPRequestOperationManager manager];
    
    NSMutableDictionary *params=[NSMutableDictionary dictionary];
    [params setObject:account.access_token forKey:@"access_token"];
    
    [mgr GET:@"https://api.weibo.com/2/statuses/friends_timeline.json" parameters:params success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) {
        
        NSArray *newStatus=[WBStatus objectArrayWithKeyValuesArray:responseObject[@"statuses"]];
        NSIndexSet *set=[[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(0, newStatus.count)];
        [self.statues insertObjects:newStatus atIndexes:set];
        [self.tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",[error localizedDescription]);
    }];

POST请求:

AFHTTPRequestOperationManager *mgr=[AFHTTPRequestOperationManager manager];
    
    NSMutableDictionary *param=[NSMutableDictionary dictionary];
    [param setObject:@"43435345453 forKey:@"client_id"];
    [param setObject:@"354083454f535fv53c53d97" forKey:@"client_secret"];
    [param setObject:@"authorization_code" forKey:@"grant_type"];
    [param setObject:@"http://www.baidu.com" forKey:@"redirect_uri"];
    [param setObject:code forKey:@"code"];
    

    [mgr POST:@"https://api.weibo.com/oauth2/access_token" parameters:param success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) {
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];

2.MJExtension用户直接处理Dictionary与Bean之间的转换

#import <Foundation/Foundation.h>
#import "WBUser.h"

@interface WBStatus : NSObject

@property (nonatomic,copy) NSString *text;

@property (nonatomic,copy) NSString *idStr;

@property (nonatomic,strong) WBUser *user;

@end
对应的json:
"statuses": [
{
"id": 11488058246,
"text": "求关注。",
...

"user": {
"id": 1404376560,
"name": "zaku",
"description": "人生五十年,乃如梦如幻;有生斯有死,壮士复何憾。",
"url": "http://blog.sina.com.cn/zaku",
"profile_image_url": "http://tp1.sinaimg.cn/1404376560/50/0/1",
...
}
},
...
]
如下示例将json转换成数据,转换如下:

#import "MJExtension.h"
NSArray *newStatus=[WBStatus objectArrayWithKeyValuesArray:jsonStr];

3.图片显示框架SDWebImage

#import "UIImageView+WebCache.h"
NSURL *url=[NSURL URLWithString:urlStr];
UIImage *placehoder = [UIImage imageNamed:@"default_image"];
[imageView sd_setImageWithURL:url placeholderImage:placehoder];


应用无内存时 关闭下载清理内存

-(void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
    SDWebImageManager *mgr=[SDWebImageManager sharedManager];
    
    [mgr cancelAll];
    
    [mgr.imageCache clearMemory];
    
}


AFNetworking与MJExtension处理后台_图片框架SDWebImage(初稿)

标签:

原文地址:http://blog.csdn.net/qq285016127/article/details/45951789

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!