zoukankan      html  css  js  c++  java
  • NSURLSessionDataTask

    #import "ViewController.h"

    @interface ViewController ()<NSURLSessionDelegate,NSURLSessionTaskDelegate,NSURLSessionDataDelegate>

    @property (weak, nonatomic) IBOutlet UIImageView *imageView;

    @property (weak, nonatomic) IBOutlet UIProgressView *progress;

    @property (nonatomic,strong) NSURLSession * session;

    @property (nonatomic,strong) NSURLSessionDataTask * dataTask;

    @property (nonatomic,strong) NSMutableData * imageData;

    @property (nonatomic,assign) long long length;

    @end

    @implementation ViewController

     

    - (void)viewDidLoad {

        [super viewDidLoad]; 

    }

    -(NSURLSession *)session{

        if (_session == nil) {

            NSURLSessionConfiguration * config = [NSURLSessionConfiguration defaultSessionConfiguration];

            self.session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue mainQueue]];

        }

        return _session;

    }

    -(NSURLSessionDataTask *)dataTask{

        if (_dataTask ==nil) {

            NSURL * url = [NSURL URLWithString:@"http://img1.3lian.com/2015/a1/124/d/181.jpg"];

            self.dataTask = [self.session dataTaskWithURL:url];

        }

        return _dataTask;

    } 

    - (IBAction)suspendenButtonDidClicked:(UIButton *)sender {

        if (self.dataTask.state == NSURLSessionTaskStateRunning) {

            [self.dataTask suspend];

        }

    }

    - (IBAction)resumeButtonDidClicked:(UIButton *)sender {

        [self.dataTask resume];

    } 

    - (IBAction)cancelButtonDidClicked:(UIButton *)sender {

        if (_dataTask) {

            if (self.dataTask.state == NSURLSessionTaskStateRunning||NSURLSessionTaskStateSuspended) {

                [self.dataTask cancel];

                self.dataTask=nil;

                self.session = nil;

                [self.progress setProgress:0 animated:NO];

            }

        }

    } 

    //获取服务器返回的响应信息

    -(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler{

        //转换

        NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *)response;

        NSLog(@"%@",[httpResponse allHeaderFields]);

        //获取资源大小

        self.length = httpResponse.expectedContentLength;

        NSLog(@"responce"); 

        if (_length!=-1) {

            //继续下载或上传

            self.imageData = [NSMutableData dataWithCapacity:(NSUInteger)_length];

            completionHandler(NSURLSessionResponseAllow);

        }else{

            completionHandler(NSURLSessionResponseCancel);

            [self.dataTask cancel];

            self.dataTask = nil;

        }

    }

    //一个数据包过来了

    -(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(nonnull NSData *)data{

        NSLog(@"receive data");

        //将当前的数据添加到imageData

        [self.imageData appendData:data];

        float rate = data.length/(_length*1.0);

        [self.progress setProgress:(_progress.progress +rate) animated:NO];

    }

    //下载完毕

    -(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error{

        NSLog(@"complete");

        if (error) {

            NSLog(@"error");

        }else{

            UIImage * img = [UIImage imageWithData:_imageData];

            self.imageView.image = img;

        }

        self.session = nil;

        self.dataTask = nil;

    }

    @end

  • 相关阅读:
    Codeforces Round 546 (Div. 2)
    Codeforces Round 545 (Div. 2)
    Codeforces Round 544(Div. 3)
    牛客小白月赛12
    Codeforces Round 261(Div. 2)
    Codeforces Round 260(Div. 2)
    Codeforces Round 259(Div. 2)
    Codeforces Round 258(Div. 2)
    Codeforces Round 257 (Div. 2)
    《A First Course in Probability》-chaper5-连续型随机变量-随机变量函数的分布
  • 原文地址:https://www.cnblogs.com/damonWq/p/5361272.html
Copyright © 2011-2022 走看看