IOS AFNetworking3.0 SDImage 网络请求和图片加载

来源:互联网 发布:.net软件开发 编辑:程序博客网 时间:2024/05/28 03:02

首先  下载AF3.0最新版本  地址:https://github.com/AFNetworking/AFNetworking

下载完成后拖入工程;引入头文件

#import "AFHTTPSessionManager.h"

如果是http请添加

  1. 在Info.plist中添加NSAppTransportSecurity类型Dictionary

  2. NSAppTransportSecurity下添加NSAllowsArbitraryLoads类型Boolean,值设为YES



开始请求

    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];

    NSDictionary *dic = @{@"UserID":@"1",@"Page":@"1",@"PageSize":@"1",@"Type":@"4"};

    [session POST:@"写入地址" parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {

        

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {


        NSString *string = [NSString stringWithFormat:@"%@",responseObject[@"retmsg"]];

        NSData *jsonData = [string dataUsingEncoding:NSUTF8StringEncoding];

        NSError *error;

       allArray = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];

        [self.oneTabelView reloadData];

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        

    }];

    [self.view addSubview:self.oneTabelView];


加载图片

需要下载SDimage这个工具,在git上先下载好

导入:

#import "UIImageView+WebCache.h"


然后直接调用:

 [cell.imageViewsd_setImageWithURL:allArray[0][@"imgUrl"]placeholderImage:[UIImageimageNamed:@"yindao04"]];

就OK了


在这里说明一点,AF也有个图片加载的SDimage,但是这个方法好像已经不行了,用的话直接就会崩溃,并且你导入了SDimage后会出现警告,警告你这个方法已经被舍弃了

0 0
原创粉丝点击