Network connection whether it is synchronous or asynchronous, its picture download is synchronous download, compared to lag, so the use of asynchronous connection is very necessary
First, figure? Download is a common feature in iOS development, but the system does not provide a diagram download class.
In order to facilitate the follow-up, you can package the diagram download to a class?? (Imagedownloader). Imagedownloader allows the outside to specify the URL, provides the ability to start downloading and cancel the download, and provides delegate or block to pass the diagram to the outside world.
Second, the agent mode of asynchronous download
Directly below the code snippet, declare the protocol in the. h file, and set the proxy
#import<Foundation/Foundation.h>#import<UIKit/UIKit.h>@classImagedownloader;@protocolImagedownloaderdelegate <NSObject>@optional- (void) Imagedownloader: (Imagedownloader *) Imagedownloader successdownloadimage: (UIImage *) image;- (void) Imagedownloader: (Imagedownloader *) Imagedownloader didfailwirherror: (Nserror *) error;@end@interfaceImagedownloader:nsobject//need for outside web site-(Instancetype) Initwithurlstr: (NSString *) urlstrDelegate:(ID<ImageDownloaderDelegate>)Delegate;//traverse the constructor for ease of use by the outside world+ (Imagedownloader *) Imagedownloaderwithurlstr: (NSString *) urlstrDelegate:(ID<ImageDownloaderDelegate>)Delegate;@end
In the. m file
#import "ImageDownloader.h"@interfaceImagedownloader () <NSURLConnectionDataDelegate>{ ID<ImageDownloaderDelegate>_delegate;//Define instance variables to use _delegate in other methods} @property (nonatomic, retain) Nsmutabledata*Receiveddata;@end@implementationImagedownloader- (void) dealloc{[_receiveddata release]; [Super Dealloc];}-(Nsmutabledata *) receiveddata{if(_receiveddata = =Nil) {Self.receiveddata= [Nsmutabledata datawithcapacity:0]; } return_receiveddata;}-(Instancetype) Initwithurlstr: (NSString *) urlstrDelegate:(ID<ImageDownloaderDelegate>)Delegate{ Self=[Super Init]; if(self) {Nsurl*url =[Nsurl Urlwithstring:urlstr]; Nsurlrequest*request =[Nsurlrequest Requestwithurl:url]; [[[Nsurlconnection alloc] Initwithrequest:requestDelegate: Self] autorelease]; _delegate=Delegate; } returnSelf ; }+ (Imagedownloader *) Imagedownloaderwithurlstr: (NSString *) urlstrDelegate:(ID<ImageDownloaderDelegate>)Delegate{Imagedownloader*imagedownloader = [[[Imagedownloader alloc] Initwithurlstr:urlstrDelegate:Delegate] autorelease]; returnImagedownloader;}- (void) Connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) response{}- (void) Connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data{[Self.receiveddata appenddata:data];}- (void) Connectiondidfinishloading: (Nsurlconnection *) connection{if(_delegate &&[_delegate respondstoselector: @selector (imagedownloader:successdownloadimage:)]) {UIImage*image =[UIImage ImageWithData:self.receivedData]; [_delegate imagedownloader:self successdownloadimage:image]; } }- (void) Connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) error{if(_delegate &&[_delegate respondstoselector: @selector (imagedownloader:didfailwirherror:)]) {Nserror*error =Nil; [_delegate imagedownloader:self Didfailwirherror:error]; } }@end
This allows us to use this class in the outside world to implement the asynchronous download function of the image.
Asynchronous download of images