• iOS 设置下载部分文件,如何获取完整文件的大小


    在视频的需求中,遇到这样一个需求,播放一视频的时候,要预下载
    后面10条视频,但是只下载后面十条视频的前面1M

    实现方法
    1 创建请求时设置cacheLength

        resource = [[IdiotResource alloc] init];
            resource.requestURL = task.requestURL;
            resource.requestOffset = task.requestOffset;
            resource.fileLength = task.fileLength;
            resource.cachePath = task.cachePath;
            //预先下载1M
            resource.cacheLength = 1024 * 1024;
            resource.resourceType = IdiotResourceTypeNet;//网络资源
            [self.resources addObject:resource];
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    二 创建请求时,设置 请求头的Range

    - (void)fetchFromNetwork:(IdiotResource *)task withResource:(IdiotResource *)resource{
        
        NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[resource.requestURL originalSchemeURL] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
        if (resource.cacheLength > 0) {
            [request addValue:[NSString stringWithFormat:@"bytes=%lld-%lld", MAX(resource.requestOffset, task.requestOffset + task.cacheLength), resource.requestOffset+resource.cacheLength-1] forHTTPHeaderField:@"Range"];
        }else{
            [request addValue:[NSString stringWithFormat:@"bytes=%lld-", resource.requestOffset] forHTTPHeaderField:@"Range"];
        }
        NSURLSessionDataTask * datatask = [self.session dataTaskWithRequest:request];
        datatask.taskDescription = [NSString stringWithFormat:@"%lld",task.requestOffset];
        [datatask resume];
        
        self.currentDataTask = datatask;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    三 如何获取完整文件的大小

    在 - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {
    代理方法里面
    通过获取响应头的 Content-Range 字段来获取,注意,不能通过
    Content-Length来获取,因为这个时候Content-Length 是我们设置的请求部分的大小,不是完整的大小

    - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {
        
        IdiotResource * task = [self.taskDic objectForKey:dataTask.taskDescription];
        
        if (task.cancel) return;
        
        if (task.fileLength <= 0) {
            NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *)response;
            NSString * contentRange = [[httpResponse allHeaderFields] objectForKey:@"Content-Range"];
            NSString * fileLength = [[contentRange componentsSeparatedByString:@"/"] lastObject];
            task.fileLength = fileLength.integerValue > 0 ? fileLength.integerValue : response.expectedContentLength;
        }
        
        if (self.currentResource.fileLength <= 0) {
            self.currentResource.fileLength = task.fileLength;
        }
        
        if (!task.cachePath.length) {
            task.cachePath = [IdiotFileManager createSliceWithUrl:task.requestURL sliceName:[NSString stringWithFormat:@"%lld-%lld",task.requestOffset,task.fileLength]];
        }
        
        if (self.currentResource.cacheLength <= 0) {
            self.currentResource.cacheLength = task.fileLength - task.requestOffset;
        }
        
        completionHandler(NSURLSessionResponseAllow);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    如下图,所示
    请添加图片描述

  • 相关阅读:
    操作系统实验四 进程间通信
    Python Flask教程学习02
    使用GoQuery实现头条新闻采集
    休闲娱乐APP,你是否感兴趣呢
    透明质酸改性乳清白蛋白/肌白蛋白/豆清白蛋白/蓖麻蛋白/豌豆白蛋白1b ( PA1b)纳米粒HA-PA1b
    JavaWeb课程设计-学生信息管理系统(Jsp+Servlet+MySql)
    HTML5期末作业:明星网站设计与实现——明星薛之谦介绍网页设计7个页面HTML+CSS+JavaScript
    hitcon_ctf_2019_one_punch(libc2.29 tcache tashing unlink)
    PCAP学习笔记二:pcap4j源码笔记
    【BSC】使用Python玩转PancakeSwap(入门篇)
  • 原文地址:https://blog.csdn.net/LIUXIAOXIAOBO/article/details/132594925