AFNetworking多任務(wù)異步請(qǐng)求

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

//多個(gè)任務(wù)異步請(qǐng)求數(shù)據(jù)
NSString * path1 = @"http://mp.manzuo.com/pic/act/banner_20150416144658_20150514104127.jpg";
//<1>轉(zhuǎn)化成NSURL
NSURL * url1 = [NSURL URLWithString:path1];
//<2>封裝成請(qǐng)求對(duì)象
NSURLRequest * request1 = [NSURLRequest requestWithURL:url1];
//<3>開(kāi)始異步請(qǐng)求
AFHTTPRequestOperation * operation1 = [[AFHTTPRequestOperation alloc]initWithRequest:request1];
//<4>請(qǐng)求結(jié)束以后 將請(qǐng)求的數(shù)據(jù)顯示在UIImageView上
[operation1 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    UIImageView * imageView = (UIImageView *)[self.view viewWithTag:1];
    //將請(qǐng)求的數(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>封裝成請(qǐng)求對(duì)象
NSURLRequest * request2 = [NSURLRequest requestWithURL:url2];
//<3>開(kāi)始異步請(qǐng)求
AFHTTPRequestOperation * operation2 = [[AFHTTPRequestOperation alloc]initWithRequest:request2];
//<4>請(qǐng)求結(jié)束以后 將請(qǐng)求的數(shù)據(jù)顯示在UIImageView上
[operation2 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    UIImageView * imageView = (UIImageView *)[self.view viewWithTag:2];
    //將請(qǐng)求的數(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>封裝成請(qǐng)求對(duì)象
NSURLRequest * request3 = [NSURLRequest requestWithURL:url3];
//<3>開(kāi)始異步請(qǐng)求
AFHTTPRequestOperation * operation3 = [[AFHTTPRequestOperation alloc]initWithRequest:request3];
//<4>請(qǐng)求結(jié)束以后 將請(qǐng)求的數(shù)據(jù)顯示在UIImageView上
[operation3 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    UIImageView * imageView = (UIImageView *)[self.view viewWithTag:3];
    //將請(qǐng)求的數(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>封裝成請(qǐng)求對(duì)象
NSURLRequest * request4 = [NSURLRequest requestWithURL:url4];
//<3>開(kāi)始異步請(qǐng)求
AFHTTPRequestOperation * operation4 = [[AFHTTPRequestOperation alloc]initWithRequest:request4];
//<4>請(qǐng)求結(jié)束以后 將請(qǐng)求的數(shù)據(jù)顯示在UIImageView上
[operation4 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    UIImageView * imageView = (UIImageView *)[self.view viewWithTag:4];
    //將請(qǐng)求的數(shù)據(jù)轉(zhuǎn)化成UIImage添加到圖片視圖上
    imageView.image = [UIImage imageWithData:responseObject];
    NSLog(@"request4");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"%@",error.description);
}];


//<5>將多個(gè)請(qǐng)求對(duì)象放在一個(gè)隊(duì)列中
NSOperationQueue * queue = [[NSOperationQueue alloc]init];
//<6>設(shè)置隊(duì)列中最大承載的請(qǐng)求對(duì)象的個(gè)數(shù)
queue.maxConcurrentOperationCount = 4;
//<7>將4個(gè)請(qǐng)求放在隊(duì)列中開(kāi)始異步請(qǐng)求
[queue addOperations:@[operation1,operation2,operation3,operation4] waitUntilFinished:NO];

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容