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

  • 相关阅读:
    pyenv
    [20200724NOIP提高组模拟T3]终章-剑之魂
    [20200724NOIP提高组模拟T2]圣章-精灵使的魔法语
    [20200724NOIP提高组模拟T1]序章-弗兰德的秘密
    [20200723NOIP提高组模拟T1]同余
    [SDOI2008]仪仗队
    P3195 [HNOI2008]玩具装箱
    [20200722NOIP提高组模拟T4]词韵
    [20200721NOIP提高组模拟T3]最小代价
    [20200721NOIP提高组模拟T1]矩阵
  • 原文地址:https://www.cnblogs.com/huoran1120/p/5291206.html
Copyright © 2011-2022 走看看