zoukankan      html  css  js  c++  java
  • 从网上DownLoad(断点下载)

    方法步骤一、storyboard布局

    #import "ViewController.h"
    
    @interface ViewController ()<NSURLSessionDownloadDelegate>
    ///显示图片的
    @property (weak, nonatomic) IBOutlet UIImageView *imageView;
    ///显示进度的
    @property (weak, nonatomic) IBOutlet UIProgressView *progressView;
    
    ///下载任务的属性
    @property (nonatomic,strong)NSURLSessionDownloadTask *task;
    ///网络请求类
    @property (nonatomic,strong)NSURLSession *session;
    ///发送请求类
    @property (nonatomic,strong)NSURLRequest *request;
    
    ///用于保存已经下载的数据的,供继续下载使用
    @property(nonatomic,strong)NSMutableData *data;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        self.progressView.progress = 0  ;
       
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    #pragma mark - 开始下载
    - (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];
        //创建NSURLSession
        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
        //网络请求
        self.session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:([NSOperationQueue mainQueue])];
        //下载请求任务
        self.task = [self.session downloadTaskWithRequest:self.request];
        
        //开启
        [self.task resume];
        
        
    }
    #pragma mark - 暂停下载
    - (IBAction)pauseButton:(id)sender {
        //暂停
        if (self.task) {
            //暂停并保存之前已经下载的内容
            __weak typeof(self)weakSelf = self;
            [self.task cancelByProducingResumeData:^(NSData * _Nullable resumeData) {
                weakSelf.data = [NSMutableData data];
            }];
        }
        [self.task suspend];
    }
    #pragma mark - 继续下载
    - (IBAction)continueButton:(id)sender {
        //哦按段当前任务是否存在,是发送请求还是处理数据
        
        if (self.task!=nil) {
            //说明已经下载,这里要处理的就是数据
            self.task = [self.session downloadTaskWithResumeData:self.data];
        }else {
            //此时没有下载任何内容,应该重新发送求求进行下载
            self.task = [self.session downloadTaskWithRequest:self.request];
        }
        //启动
        [self.task resume];
        
    }
    #pragma mark - 代理方法
    //下载完成走的方法
    - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
        //找到文件夹管理器
        
        NSFileManager *fileManager = [NSFileManager defaultManager];
        
        //先找到沙盒
        NSArray *urlArray = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
        
        //根据沙盒路径获取Document路径
        
        NSURL *url = [urlArray objectAtIndex:0];
        NSLog(@"url=====%@",url);
        
    //    response.suggestedFilename:建议使用的文件名,一般根服务端的文件名一致
        
        NSURL *saveUrl = [url URLByAppendingPathComponent:downloadTask.response.suggestedFilename];
        
        NSLog(@"sabrUrl -------%@",saveUrl);
        //location:临时文件的路径(下载好的文件)
        //AtPath:剪切前的文件路径 //ToPath:剪切后的文件路径
        [fileManager moveItemAtPath:location.path toPath:saveUrl.path error:nil];
        self.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:saveUrl]];
      
    }
    
    #pragma mark - 下载完,部分就会调用(可能被调用多次)
    
    - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
        
        //下载当前段的数据
        NSLog(@"bytesWrite = %lld",bytesWritten);
        NSLog(@"totalytesWritten = %lld",totalBytesWritten);
        NSLog(@"titalBytesExpectedToWrite = %lld",totalBytesExpectedToWrite);
        
        
        
        self.progressView.progress = (CGFloat)totalBytesWritten /(CGFloat)totalBytesExpectedToWrite;
        
        
        NSLog(@"%f",self.progressView.progress);
    }
    
    
    @end

  • 相关阅读:
    [洛谷P4248][AHOI2013]差异
    [洛谷P4070][SDOI2016]生成魔咒
    [洛谷P3979]遥远的国度
    [CF551E]GukiZ and GukiZiana
    [洛谷P4721]【模板】分治 FFT_求逆
    [洛谷P4721]【模板】分治 FFT
    一键智能抠图-原理与实现
    国防科技大学单张RGB-D图像预测物体对称性
    在OpenCV中使用色彩校正
    Deformable DETR DETR 存在收敛速度慢等缺陷。为了解决这些问题,本文可变形 DETR,其注意力模块仅关注于参考点附近的一小部分采样点作为注意力模块中的 key 元素。可变形 DETR 可以在比 DETR 少 9/10 的训练轮数下,达到更好的性能(尤其是在小目标上)。在 COCO 基准上的大量实验表明了该方法的有效性。
  • 原文地址:https://www.cnblogs.com/mingjieLove00/p/5544073.html
Copyright © 2011-2022 走看看