【2012.08.03】对AFNetworking的学…

继ASIHTTPRequest发布不在维护的讯息后,如果我们不使用CDN(云端服务器),AFNetworking 是一套不错的选择。
下载网址:https://github.com/AFNetworking/AFNetworking

下载之后,直接进入Xcode的专案即可以用,记得加入SystemConfiguration.framework

范例參考:

在application: didFinishLaunchingWithOptions: 加入AFNetworkActivityIndicatorManager
记得 #import "AFNetworkActivityIndicatorManager.h"

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
    self.window.backgroundColor = [UIColor whiteColor];
 
    [self.window makeKeyAndVisible];
    return YES;
}

这是我们常用的下载网路资源(JSON 格式)
记得
#import "AFHTTPClient.h"
#import "AFHTTPRequestOperation.h"
#import "JSONKit.h"

    NSURL *url = [NSURL URLWithString:@"http://www.domain.com"];
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
    NSString *_path=[NSString stringWithFormat:@"/user_login/%@/%@/",userName,password];
    NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:_path parameters:nil];
    [httpClient release];
 
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
 
    [operation  setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        //下載成功之後,使用JSONKit將字串轉成NSDictionary或NSArray 格式
        NSDictionary *deserializedData = [operation.responseString objectFromJSONString];
 
    }
     failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          //下載失敗之後處理
    }];
 
    NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
    [queue addOperation:operation];

我们做完上一个步驟,有時候会得到一些图片的绝对网址,接下来就是根据这些网址,进行非同步下载图片。
記得 #import "UIImageView+AFNetworking.h"
简单的做法,是

[imageView setImageWithURL:@"圖片的絕對路徑"];

複雜的做法,可以在圖片下載完成之後,再去觸發一些事件

NSURL *url = [NSURL URLWithString:@"圖片的絕對路徑"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]
[imageView setImageWithURLRequest:request placeholderImage:nil 
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) 
{ 
    NSLog(@"圖片下載成功!do something")
} 
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) 
{ 
       NSLog(@"圖片無法下載!do something""); 
}];                                

看了 iKevin 的 《AFNetworking 初探》这篇笔记(以上内容)之后,我在目前做的项目中,也用到了。我附上我部分的相关内容的代码,供大家参考下。iKevin的博客: http://ikevin.tw/308


if  (view  ==  nil){
    //create a view
    view  [[UIImageView  alloc]  initWithFrame:CGRectMake( 0 0 180.0f 200.0f)];
  // ActivityViewController 是一个图片倒影特效的一个头文件
   ActivityViewController *activityView [[ActivityViewController alloc] init];

   
   NSURL *url [NSURL URLWithString:RecommActivtyHost];
   NSMutableURLRequest *request [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicytimeoutInterval: 30.0 ];
   
   
    if  (activityView.view.superview == nil) {
       [view addSubview:activityView.view];
//activityView.refActivityImage 为倒影图片
   [activityView.refActivityImage setImage:[[UIImage imageNamed: @"bg_photo.png" reflectionWithAlpha: 0.3 ]];
//activityView.activityImage 为正常图片
       [activityView.activityImage setImageWithURLRequest:request 
                                         placeholderImage:[UIImage imageNamed: @"bg_photo.png"
                                                  success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
                                                      [activityView.refActivityImage setImage:[[activityView.activityImage image] reflectionWithAlpha: 0.3 ]];
                                                  
                                                  failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                                                      [activityView.refActivityImage setImage:[[UIImage imageNamed: @"bg_photo.png" reflectionWithAlpha: 0.3 ]];
                                                  }];
   }

PS:初学者,还有很多地方不是很理解,如果有同道中人可以加QQ:45773450 以后一起讨论和研究。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值