[Code Note] iOS-HTTPQueue downloads images,

Source: Internet
Author: User

[Code Note] iOS-HTTPQueue downloads images,

1. Engineering Drawing.

Ii. Code.

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 download image # import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib .} // click anywhere to download the image-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {if (! _ NetworkQueue) {_ networkQueue = [[ASINetworkQueue alloc] init];} // stop a previous queue [_ networkQueue cancelAllOperations]; // create an ASI queue [_ networkQueue setDelegate: self]; [_ networkQueue setRequestDidFinishSelector: @ selector (requestFinished :)]; [_ networkQueue failed: @ selector (requestFailed :)]; [_ networkQueue failed: @ selector (queueFinished :)]; for (int I = 1; I <3; I ++) {NSSt Ring * strURL = [[NSString alloc] initWithFormat: @ "http://iosbook3.com/service/download.php? Response ", @" <your iosbook3.com user email> ", 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 = [NSJSONSeria Lization 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 = [[delealloc] initWithTitle: @ "error message" message: errorStr delegate: nil cancelButtonTitle: @ "OK" response: nil]; [alertView show];} if ([_ networkQueue requestsCount] = 0) {[self setNetworkQueue: nil];} NSLog (@ "request succeeded ");} -(void) requestFailed :( ASIHTTPRequest *) request {NSError * error = [request error]; NSLog (@ "% @", [error localizedDescription]); if ([_ networkQueue requestsCount] = 0) {[self setNetworkQueue: nil];} NSLog (@ "request failed");}-(void) queueFinished :( ASIHTTPRequest *) request {if ([_ networkQueue requestsCount] = 0) {[self setNetworkQueue: nil];} NSLog (@ "queue completed");}-(void) didReceiveMemoryWarning {[super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated .} @ end

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.