AFNetworking與MJExtension處理後台_圖片框架SDWebImage(初稿),
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]; }