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];

二 创建请求时,设置 请求头的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;
}

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

在 - (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);
}

如下图,所示

相关推荐
绝世唐门三哥2 小时前
Mac 免费 GIF 录制软件全攻略:下载、安装与使用指南
macos·gif
2501_915918412 小时前
使用 HBuilder 上架 iOS 应用时常见的问题与应对方式
android·ios·小程序·https·uni-app·iphone·webview
2501_916007474 小时前
iOS 崩溃日志的分析方法,将崩溃日志与运行过程结合分析
android·ios·小程序·https·uni-app·iphone·webview
2501_916007474 小时前
React Native 混淆在真项目中的方式,当 JS 和原生同时暴露
javascript·react native·react.js·ios·小程序·uni-app·iphone
雪域迷影4 小时前
macOS系统上或首次使用Python的urllib模块时出现 ssl.SSLCertVerificationError 错误
python·macos·ssl
00后程序员张5 小时前
苹果应用商店上架App流程,签名证书、IPA 校验、上传
android·ios·小程序·https·uni-app·iphone·webview
2501_916007475 小时前
iOS 上架需要哪些准备,围绕证书、描述文件和上传方式等关键环节展开分析
android·ios·小程序·https·uni-app·iphone·webview
2501_915106325 小时前
iOS 上架费用解析,哪些成本可以通过流程优化降低。
android·ios·小程序·https·uni-app·iphone·webview
皇上o_O5 小时前
Swift 新并发框架之 async/await
ios
TheNextByte16 小时前
如何将文件从iPhone传输到USB闪存盘?
ios·iphone