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

  • 相关阅读:
    Prometheus监控告警浅析
    BaikalDB技术实现内幕(三)--代价模型实现
    微服务的熔断原理与实现
    2020双11,阿里巴巴集团数万数据库系统全面上云揭秘
    平行进化论再添证据 牙形刺远隔千里却发育模式相同
    如何利用设计模式改善业务代码?
    SpringBoot 无侵入式实现 API 接口统一 JSON 格式返回
    独家 | 这可能会引领通用AI的下一个重大突破
    iOS 网络优化: 使你的 App 网络交互更流畅
    Java Web整合开发(17) -- Struts 2.x 高级应用
  • 原文地址:https://www.cnblogs.com/damonWq/p/5361272.html
Copyright © 2011-2022 走看看