# Import <Foundation/Foundation. h> @ Class myconnection; // download protocol @ protocol myconnectiondelegate <nsobject> // download completed-(void) myconnectiondidfinish :( myconnection *) con data :( nsdata *) data; // download failed-(void) myconnectiondidfail :( myconnection *) con; @ end @ interface myconnection: nsobject <timeout >{ nsmutabledata * imagedata;}-(ID) initwithurl :( nsstring *) URL delegate :( ID) Delegate; @ property (nonatomic, assign) ID <myconnectiondelegate> delegate; @ property (nonatomic, retain) nsstring * URL; @ property (nonatomic, assign) int tag; @ end # import "myconnection. H "# import" nsstring + hashing. H "@ implementation myconnection @ synthesize url = _ URL; @ synthesize delegate = _ delegate; @ synthesize tag = _ Tag;-(ID) initwithurl :( nsstring *) URL delegate :( ID) delegate {If (Self = [Super init]) {self. delegate = delegate; self. url = URL; imagedata = [[nsmutabledata alloc] init]; nsurlrequest * request = [nsurlrequest requestwithurl: [nsurl failed: url] cachepolicy: nsurlrequestreloadignoringlocalcachedata timeoutinterval: 30.0]; [nsurlconnection connectionwithrequest: request delegate: Self];} return self;}-(void) connection :( nsurlconnection *) connection didreceiveresponse :( nsurlresponse *) response {[uiapplication sharedapplication]. response = yes;}-(void) connection :( nsurlconnection *) connection didreceivedata :( nsdata *) Data {[imagedata appenddata: Data];}-(void) connectiondidfinishloading :( nsurlconnection *) connection {[uiapplication sharedapplication]. networkactivityindicatorvisible = no; // cache // http: // 192.168.88.8/wer/1.png // MD5 base64 // var/user/24234234/tmp/kjhaksdf87asd7f8a7sd8f78as7df // http: // 192.168.88.8/2.png // base64 // aahttp: // www.baidu.com/1.rarzz // thrund: // Signature = nsstring * Path = nshomedirectory (); Path = [nsstring stringwithformat: @ "% @/tmp/% @", path, [self. URL md5hash]; [imagedata writetofile: path atomically: No]; // use the downloaded proxy [_ delegate myconnectiondidfinish: Self data: imagedata];}-(void) connection :( nsurlconnection *) connection didfailwitherror :( nserror *) error {[uiapplication sharedapplication]. networkactivityindicatorvisible = no; // use the failed download proxy [_ delegate myconnectiondidfail: Self];}-(void) dealloc {[imagedata release]; self. url = nil; [Super dealloc] ;}@ end