One, engineering drawings.
Two, code.
ViewController.h
#import <UIKit/UIKit.h> #import Asihttprequest.h " #import "asinetworkqueue.h" Span style= "COLOR: #0000ff" > #import "nsnumber+message.h " #import " nsstring+urlencoding.h" @interface Span style= "COLOR: #000000" > Viewcontroller:uiviewcontroller@property (nonatomic,strong) asinetworkqueue *networkqueue; @end
Viewcontroller.m
//Asinetworkqueue Download Image#import"ViewController.h"@interfaceViewcontroller ()@end@implementationviewcontroller-(void) Viewdidload {[Super viewdidload];//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 the previous queue[_networkqueue cancelalloperations];//Create an ASI queue[_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",@"< your iosbook3.com user mailbox >", 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:@"Error message"Message:errorstrDelegate: Nil Cancelbuttontitle:@"Ok"Otherbuttontitles: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 @ " queue complete " void) didreceivememorywarning {[Super Didreceivememorywarning]; // Dispose of any resources this can be Recreated.} @end
"Code Note" Ios-httpqueue Download image