IOS 练习 之 大文件下载优化


// NSMutableData 的append 方式下载会将数据放入到内存中,等待文件下载完成后才会进行写入操作,这样一来,app中的内存将会非常大,等同于下载的文件大小。
// 所以可以使用句柄控制写入,即下载一部分数据就写入到存储介质中。这样可以大量节省内存占用
//  ViewController.m
//  DownLoadBig
//
//  Created by apple on 15/5/5.
//  Copyright (c) 2015年 apple. All rights reserved.
//
#import "ViewController.h"
@interface ViewController () <NSURLConnectionDataDelegate,UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UIProgressView *proView;
@property (nonatomic,strong) NSMutableData *fileData;
@property (nonatomic,copy) NSString *total;
@property (nonatomic,strong) UIAlertView *alert;
@property (nonatomic,strong) NSFileHandle *wHandle;
@property (nonatomic,assign) NSInteger currentLength;
@property (nonatomic,strong) NSString *urlstr;
@end
@implementation ViewController
- (NSString *)urlstr
{
    if (_urlstr == nil){
        _urlstr = @"http://124.205.69.135/files/8135000000D8446F/trial2.autodesk.com/NET16SWDLD/2016/ACD/DLM/AutoCAD_2016_Simplified_Chinese_Win_32bit_dlm.sfx.exe";
    }
    return _urlstr;
}
-(NSMutableData *)fileData
{
    
    if (_fileData == nil){
        
        _fileData = [NSMutableData data];
    }
    return _fileData;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.proView.progress = 0.0;
    
    NSString *AppName =  [[self.urlstr componentsSeparatedByString:@"/"] lastObject];
    NSString *cachestr = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filePath = [cachestr stringByAppendingPathComponent:AppName];
    NSFileManager *mgr = [NSFileManager defaultManager];
    
    [mgr createFileAtPath:filePath contents:nil attributes:nil];
    self.wHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
    
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSURL *url = [NSURL URLWithString:self.urlstr];
//
    
//    NSURL *url = [NSURL URLWithString:@"http://125.39.68.200/files/2011000000F9723B/xiazai.888rj.com/Soft/T/Thunder_7.9.26.4824_XiaZaiBa.exe"];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    
    [NSURLConnection connectionWithRequest:req delegate:self];
//    NSLog(@"touch");
    
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse *res = (NSHTTPURLResponse *)response;
//    NSLog(@"jieshou");
//    NSLog(@"%@%",res.allHeaderFields[@"Content-Length"]);
     self.total= res.allHeaderFields[@"Content-Length"];
//    self.total = res.expectedContentLength
    
    
    
    
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    
//    [self.fileData appendData:data];
    [self.wHandle seekToEndOfFile];// 寻找文件的末尾偏移量
    [self.wHandle writeData:data];
    self.currentLength += data.length;
    
    self.proView.progress = (double)self.currentLength  / self.total.intValue;
    NSLog(@"%d%",self.currentLength);
    
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    
  
//    [self.fileData writeToFile:filePath atomically:YES];
//    /Users/apple/Library/Developer/CoreSimulator/Devices/612DC518-63EA-4614-AA42-30CAECF2B57B/data/Containers/Data/Application/77095FDC-6D9B-4BB6-AC5F-460EB80DBD22/Library/Caches
    
    NSString *totalstr = [ NSString stringWithFormat:@"软件总大小:%@",self.total];
    self.alert = [[UIAlertView alloc]  initWithTitle:@"下载完成" message:totalstr delegate:self cancelButtonTitle:@"ok"  otherButtonTitles: nil ];
    
    [self.alert show];
}
@end

你可能感兴趣的:(IOS 练习 之 大文件下载优化)