美文网首页
AFNetworking多任务异步请求

AFNetworking多任务异步请求

作者: 木马不在转 | 来源:发表于2016-10-28 11:11 被阅读385次

//方法
-(void)pressBarBtn:(id)sender
{

//多个任务异步请求数据
NSString * path1 = @"http://mp.manzuo.com/pic/act/banner_20150416144658_20150514104127.jpg";
//<1>转化成NSURL
NSURL * url1 = [NSURL URLWithString:path1];
//<2>封装成请求对象
NSURLRequest * request1 = [NSURLRequest requestWithURL:url1];
//<3>开始异步请求
AFHTTPRequestOperation * operation1 = [[AFHTTPRequestOperation alloc]initWithRequest:request1];
//<4>请求结束以后 将请求的数据显示在UIImageView上
[operation1 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    UIImageView * imageView = (UIImageView *)[self.view viewWithTag:1];
    //将请求的数据转化成UIImage添加到图片视图上
    imageView.image = [UIImage imageWithData:responseObject];
    NSLog(@"request1");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"%@",error.description);
}];
NSString * path2 = @"http://10.0.8.8/sns/attachment/201412/20/93916_1419040676yd6Q.jpg";
//<1>转化成NSURL
NSURL * url2 = [NSURL URLWithString:path2];
//<2>封装成请求对象
NSURLRequest * request2 = [NSURLRequest requestWithURL:url2];
//<3>开始异步请求
AFHTTPRequestOperation * operation2 = [[AFHTTPRequestOperation alloc]initWithRequest:request2];
//<4>请求结束以后 将请求的数据显示在UIImageView上
[operation2 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    UIImageView * imageView = (UIImageView *)[self.view viewWithTag:2];
    //将请求的数据转化成UIImage添加到图片视图上
    imageView.image = [UIImage imageWithData:responseObject];
    NSLog(@"request2");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"%@",error.description);
}];


NSString * path3 = @"http://mp.manzuo.com/pic/act/banner_20150520104032.jpg";
//<1>转化成NSURL
NSURL * url3 = [NSURL URLWithString:path3];
//<2>封装成请求对象
NSURLRequest * request3 = [NSURLRequest requestWithURL:url3];
//<3>开始异步请求
AFHTTPRequestOperation * operation3 = [[AFHTTPRequestOperation alloc]initWithRequest:request3];
//<4>请求结束以后 将请求的数据显示在UIImageView上
[operation3 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    UIImageView * imageView = (UIImageView *)[self.view viewWithTag:3];
    //将请求的数据转化成UIImage添加到图片视图上
    imageView.image = [UIImage imageWithData:responseObject];
    NSLog(@"request3");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"%@",error.description);
}];


NSString * path4 = @"http://mp.manzuo.com/pic/act/banner_20150518145310.jpg";
//<1>转化成NSURL
NSURL * url4 = [NSURL URLWithString:path4];
//<2>封装成请求对象
NSURLRequest * request4 = [NSURLRequest requestWithURL:url4];
//<3>开始异步请求
AFHTTPRequestOperation * operation4 = [[AFHTTPRequestOperation alloc]initWithRequest:request4];
//<4>请求结束以后 将请求的数据显示在UIImageView上
[operation4 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    UIImageView * imageView = (UIImageView *)[self.view viewWithTag:4];
    //将请求的数据转化成UIImage添加到图片视图上
    imageView.image = [UIImage imageWithData:responseObject];
    NSLog(@"request4");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"%@",error.description);
}];


//<5>将多个请求对象放在一个队列中
NSOperationQueue * queue = [[NSOperationQueue alloc]init];
//<6>设置队列中最大承载的请求对象的个数
queue.maxConcurrentOperationCount = 4;
//<7>将4个请求放在队列中开始异步请求
[queue addOperations:@[operation1,operation2,operation3,operation4] waitUntilFinished:NO];

}

相关文章

网友评论

      本文标题:AFNetworking多任务异步请求

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