//方法
-(void)pressBarBtn:(id)sender
{
//多個(gè)任務(wù)異步請求數(shù)據(jù)
NSString * path1 = @"http://mp.manzuo.com/pic/act/banner_20150416144658_20150514104127.jpg";
//<1>轉(zhuǎn)化成NSURL
NSURL * url1 = [NSURL URLWithString:path1];
//<2>封裝成請求對象
NSURLRequest * request1 = [NSURLRequest requestWithURL:url1];
//<3>開始異步請求
AFHTTPRequestOperation * operation1 = [[AFHTTPRequestOperation alloc]initWithRequest:request1];
//<4>請求結(jié)束以后 將請求的數(shù)據(jù)顯示在UIImageView上
[operation1 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
UIImageView * imageView = (UIImageView *)[self.view viewWithTag:1];
//將請求的數(shù)據(jù)轉(zhuǎn)化成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>轉(zhuǎn)化成NSURL
NSURL * url2 = [NSURL URLWithString:path2];
//<2>封裝成請求對象
NSURLRequest * request2 = [NSURLRequest requestWithURL:url2];
//<3>開始異步請求
AFHTTPRequestOperation * operation2 = [[AFHTTPRequestOperation alloc]initWithRequest:request2];
//<4>請求結(jié)束以后 將請求的數(shù)據(jù)顯示在UIImageView上
[operation2 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
UIImageView * imageView = (UIImageView *)[self.view viewWithTag:2];
//將請求的數(shù)據(jù)轉(zhuǎn)化成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>轉(zhuǎn)化成NSURL
NSURL * url3 = [NSURL URLWithString:path3];
//<2>封裝成請求對象
NSURLRequest * request3 = [NSURLRequest requestWithURL:url3];
//<3>開始異步請求
AFHTTPRequestOperation * operation3 = [[AFHTTPRequestOperation alloc]initWithRequest:request3];
//<4>請求結(jié)束以后 將請求的數(shù)據(jù)顯示在UIImageView上
[operation3 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
UIImageView * imageView = (UIImageView *)[self.view viewWithTag:3];
//將請求的數(shù)據(jù)轉(zhuǎn)化成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>轉(zhuǎn)化成NSURL
NSURL * url4 = [NSURL URLWithString:path4];
//<2>封裝成請求對象
NSURLRequest * request4 = [NSURLRequest requestWithURL:url4];
//<3>開始異步請求
AFHTTPRequestOperation * operation4 = [[AFHTTPRequestOperation alloc]initWithRequest:request4];
//<4>請求結(jié)束以后 將請求的數(shù)據(jù)顯示在UIImageView上
[operation4 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
UIImageView * imageView = (UIImageView *)[self.view viewWithTag:4];
//將請求的數(shù)據(jù)轉(zhuǎn)化成UIImage添加到圖片視圖上
imageView.image = [UIImage imageWithData:responseObject];
NSLog(@"request4");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@",error.description);
}];
//<5>將多個(gè)請求對象放在一個(gè)隊(duì)列中
NSOperationQueue * queue = [[NSOperationQueue alloc]init];
//<6>設(shè)置隊(duì)列中最大承載的請求對象的個(gè)數(shù)
queue.maxConcurrentOperationCount = 4;
//<7>將4個(gè)請求放在隊(duì)列中開始異步請求
[queue addOperations:@[operation1,operation2,operation3,operation4] waitUntilFinished:NO];
}