URL Download delegate

--显示进度条,逐渐显示已下载的图片
#import
"ViewController.h" @interface ViewController ()<NSURLSessionDelegate,NSURLSessionTaskDelegate,NSURLSessionDataDelegate> @property (weak, nonatomic) IBOutlet UIProgressView *progressView; @property (weak, nonatomic) IBOutlet UIImageView *imageView; @property (nonatomic,strong) NSURLSession *session; @property (nonatomic,strong) NSURLSessionDataTask *dataTask; @property (nonatomic,strong) NSMutableData *imageData;//保存图片数据 @property (nonatomic,assign) long long totalLength;//数据总长度 @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } -(NSURLSession *)session{ if (_session ==nil) { NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration ephemeralSessionConfiguration]; self.session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:[NSOperationQueue mainQueue]]; } return _session; } -(NSURLSessionDataTask *)dataTask{ if (_dataTask == nil) { NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_01.mp4"]; self.dataTask = [self.session dataTaskWithURL:url]; } return _dataTask; } - (IBAction)CancelButtonClicked:(id)sender { if (_dataTask) { if (self.dataTask.state == NSURLSessionTaskStateRunning ||self.dataTask.state == NSURLSessionTaskStateSuspended) { [self.dataTask cancel]; self.dataTask = nil; self.session = nil; } } } - (IBAction)SuspendedButtonClicked:(id)sender { if (self.dataTask.state == NSURLSessionTaskStateRunning ) { [self.dataTask suspend]; } } - (IBAction)ResumeButtonClicked:(id)sender { //开启下载任务 [self.dataTask resume]; } #pragma mark -----------------delegate------------------- //获取服务器返回的相应信息 -(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler{ //将urlresponse ->nshttpurlresponse NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; //获取资源的大小 self.totalLength = httpResponse.expectedContentLength; //创建保存这个数据的容器 self.imageData = [NSMutableData dataWithCapacity:_totalLength]; //确定下面如何操作 if (_totalLength != -1) { //继续下载或者上传任务 completionHandler(NSURLSessionResponseAllow); }else{ //取消数据传输,请求的数据没有 completionHandler(NSURLSessionResponseCancel); [self.dataTask cancel]; self.dataTask = nil; } } //一个数据包过来了 -(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data{ //将当前的这个数据添加到imageData [self.imageData appendData:data]; //计算进度比例,数据包的百分比 float rate = data.length/(_totalLength*1.0); //进度条改变 [self.progressView setProgress:(_progressView.progress + rate) animated:YES]; } //下载完毕 -(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error{ NSLog(@"complete"); if (error) { NSLog(@"error 下载失败"); }else{ UIImage *image = [UIImage imageWithData:_imageData]; self.imageView.image = image; } self.session = nil; self.dataTask = nil; } @end

原文地址:https://www.cnblogs.com/yangqinglong/p/5375237.html