Method Step One, storyboard layout
#import "ViewController.h"@interfaceViewcontroller () <NSURLSessionDownloadDelegate>///display the picture's@property (Weak, nonatomic) Iboutlet Uiimageview *ImageView;///show the progress of@property (Weak, nonatomic) Iboutlet Uiprogressview *Progressview;///Properties of the download task@property (nonatomic,strong) Nsurlsessiondownloadtask *task;///Network Request Class@property (nonatomic,strong) nsurlsession *session;///Send Request Class@property (nonatomic,strong) nsurlrequest *request;///used to save downloaded data for continued download use@property (nonatomic,strong) Nsmutabledata *data;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.Self.progressView.progress =0 ; }- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}#pragmaMark-Start Download-(ibaction) Startbutton: (ID) Sender {NSString*urlstr =@"http://f.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=2b7d8fd27fd98d1076810435140f9438/ 503d269759ee3d6d505b9ee540166d224f4ade7a.jpg"; Nsurl*url =[Nsurl Urlwithstring:urlstr]; Self.request=[Nsurlrequest Requestwithurl:url]; //Create NsurlsessionNsurlsessionconfiguration *configuration =[Nsurlsessionconfiguration defaultsessionconfiguration]; //Network RequestsSelf.session = [Nsurlsession sessionwithconfiguration:configurationDelegate: Self delegatequeue: ([Nsoperationqueue mainqueue]); //Download Request TaskSelf.task =[Self.session downloadTaskWithRequest:self.request]; //Open[Self.task resume]; }#pragmaMark-Pause Download-(ibaction) Pausebutton: (ID) Sender {//Pause if(self.task) {//pause and save content that was previously downloaded__weaktypeof(self) weakself =Self ; [Self.task cancelbyproducingresumedata:^ (NSData *_nullable Resumedata) {Weakself.data=[Nsmutabledata data]; }]; } [Self.task suspend];}#pragmaMark-Continue download-(ibaction) Continuebutton: (ID) Sender {//Oh, yes. By Segment the current task exists, whether to send the request or to process the data if(self.task!=Nil) { //instructions have been downloaded, here is the data to be processedSelf.task =[Self.session DownloadTaskWithResumeData:self.data]; }Else { //no content is downloaded at this time and should be re-sent for downloadSelf.task =[Self.session downloadTaskWithRequest:self.request]; } //Start[Self.task resume]; }#pragmaMark-Proxy method//Download the way to complete the walk- (void) Urlsession: (Nsurlsession *) session Downloadtask: (Nsurlsessiondownloadtask *) downloadtask Didfinishdownloadingtourl: (Nsurl *) Location {//Locate the folder managerNsfilemanager*filemanager =[Nsfilemanager Defaultmanager]; //Find the sandbox first .Nsarray *urlarray =[FileManager urlsfordirectory:nsdocumentdirectory indomains:nsuserdomainmask]; //get document path based on sandbox pathNsurl*url = [Urlarray objectatindex:0]; NSLog (@"url=====%@", URL); //response.suggestedfilename: Recommended file name, common root server file name consistentNsurl*saveurl =[url URLByAppendingPathComponent:downloadTask.response.suggestedFilename]; NSLog (@"sabrurl-------%@", Saveurl); //Location: The path to the temporary file (downloaded file)//atpath: File path before clipping//Topath: The file path after clipping[FileManager MoveItemAtPath:location.path toPath:saveUrl.path Error:nil]; Self.imageView.image=[UIImage imagewithdata:[nsdata Datawithcontentsofurl:saveurl]; }#pragmaMark-After downloading, the part will be called (may be called multiple times)-(void) Urlsession: (Nsurlsession *) session Downloadtask: (Nsurlsessiondownloadtask *) Downloadtask Didwritedata: (int64_t) Byteswritten Totalbyteswritten: (int64_t) Totalbyteswritten Totalbytesexpectedtowrite: (int64_t) Totalbytesexpectedtowrite {//download data for the current segmentNSLog (@"byteswrite =%lld", Byteswritten); NSLog (@"Totalyteswritten =%lld", Totalbyteswritten); NSLog (@"titalbytesexpectedtowrite =%lld", Totalbytesexpectedtowrite); Self.progressView.progress= (cgfloat) Totalbyteswritten/(cgfloat) totalbytesexpectedtowrite; NSLog (@"%f", self.progressView.progress);}@end
Download from the Internet