AFNetworkingマルチタスク非同期要求
3557 ワード
//方法-(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];
}