美文网首页
AFNetworking的用法

AFNetworking的用法

作者: Dove_Q | 来源:发表于2016-10-05 20:36 被阅读31次

此方法的GET和POST方法只能解析JSON数据

#import "AFNetworking.h"
{    
    NSString *api = @"http://api.class.room/?method=city&areacode=0101";
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
   //只能解析JSON格式,若为html,则须设置一下
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    [manager GET:api parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"responseObject: %@", responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error: %@", error);
    }];
}

根据URL获取图片

#import "UIImageView+AFNetworking.h"
{
    UIImageView *imageView = [UIImageView new];
    imageView.frame = CGRectMake(0, 0, 200, 200);
    imageView.center = self.view.center;
    [self.view addSubview:imageView];
    
    NSString *imageApi = @"http://atth.eduu.com/album/201203/12/1475134_1331559643qMzc.jpg";
    [imageView setImageWithURL:[NSURL URLWithString:imageApi]];

button根据URL获取图片

#import "UIButton+AFNetworking.h"

{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 100, 100);
    button.center = self.view.center;
    button.backgroundColor = [UIColor grayColor];
    button.layer.cornerRadius = 50;
    //裁剪掉多余的部分
    button.clipsToBounds = YES;
    [button addTarget:self action:@selector(didClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    [button setImageForState:UIControlStateNormal withURL:[NSURL URLWithString:imageApi]];
    
}

- (void)didClick {
    self.view.backgroundColor = [UIColor cyanColor];
}

相关文章

网友评论

      本文标题:AFNetworking的用法

      本文链接:https://www.haomeiwen.com/subject/vcxeyttx.html