Asynchronous download of images

Source: Internet
Author: User

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

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.