【代碼筆記】iOS-HTTPQueue下載圖片

來源:互聯網
上載者:User

標籤:interface   work   img   sse   成功   button   init   span   too   

一,工程圖。

二,代碼。

ViewController.h

#import <UIKit/UIKit.h>#import "ASIHTTPRequest.h"#import "ASINetworkQueue.h"#import "NSNumber+Message.h"#import "NSString+URLEncoding.h"@interface ViewController : UIViewController@property (nonatomic,strong) ASINetworkQueue *networkQueue;@end

 

ViewController.m

//ASINetworkQueue下載圖片#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.}//點擊任何處,進行圖片下載-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ if (!_networkQueue) { _networkQueue = [[ASINetworkQueue alloc] init]; } // 停止以前的隊列 [_networkQueue cancelAllOperations]; // 建立ASI隊列 [_networkQueue setDelegate:self]; [_networkQueue setRequestDidFinishSelector:@selector(requestFinished:)]; [_networkQueue setRequestDidFailSelector:@selector(requestFailed:)]; [_networkQueue setQueueDidFinishSelector:@selector(queueFinished:)]; for (int i=1; i<3; i++) { NSString *strURL = [[NSString alloc] initWithFormat:@"http://iosbook3.com/service/download.php?email=%@&FileName=test%i.jpg",@"<你的iosbook3.com使用者郵箱>",i]; NSURL *url = [NSURL URLWithString:[strURL URLEncodedString]]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; request.tag = i; [_networkQueue addOperation:request]; } [_networkQueue go];}- (void)requestFinished:(ASIHTTPRequest *)request{ NSData *data = [request responseData]; NSError *eror; NSDictionary *resDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&eror]; if (!resDict) { UIImage *img = [UIImage imageWithData:data]; if (request.tag ==1) { // _imageView1.image = img; NSLog(@"---img--%@",img); } else { //_imageView2.image = img; NSLog(@"---img--%@",img); } } else { NSNumber *resultCodeObj = [resDict objectForKey:@"ResultCode"]; NSString *errorStr = [resultCodeObj errorMessage]; UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"錯誤資訊" message:errorStr delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alertView show]; } if ([_networkQueue requestsCount] == 0) { [self setNetworkQueue:nil]; } NSLog(@"請求成功");}- (void)requestFailed:(ASIHTTPRequest *)request{ NSError *error = [request error]; NSLog(@"%@",[error localizedDescription]); if ([_networkQueue requestsCount] == 0) { [self setNetworkQueue:nil]; } NSLog(@"請求失敗");}- (void)queueFinished:(ASIHTTPRequest *)request{ if ([_networkQueue requestsCount] == 0) { [self setNetworkQueue:nil]; } NSLog(@"隊列完成");}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end

【代碼筆記】iOS-HTTPQueue下載圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.