下载功能的实现

简单地视频下载实现, 包含进度条.

@interface DownloadViewController ()<NSURLConnectionDataDelegate>


@property (strong, nonatomic) UIProgressView *progress;

@property (strong, nonatomic) NSFileHandle *writeHandle;

@property (assign, nonatomic) long long currentLength;

@property (assign, nonatomic) long long sumLength;


@end


@implementation DownloadViewController


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{

    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {

        

    }

    return self;

}


- (UIProgressView *)progress{

    if (!_progress) {

        _progress = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];

        _progress.backgroundColor = [UIColor redColor];

        _progress.frame = CGRectMake(30, 100, self.view.frame.size.height - 100, 20);

        _progress.progressTintColor = [UIColor whiteColor];

        _progress.trackTintColor = [UIColor blackColor];

        _progress.progress = 0;

    }

    return _progress;

}



- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor greenColor];

    

    

    [self.view addSubview:self.progress];

    

    [self createConnection];

}


- (void) createConnection{

    NSURL *url = [NSURL URLWithString:@"http://vmovier.qiniudn.com/eea389000bc9187034f99788df6d13e6.mp4"];

    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

    

    [NSURLConnection connectionWithRequest:request delegate:self];


}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

    NSString *cache = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

    

    NSString *filePath = [cache stringByAppendingPathComponent:@"man.mp4"];

    NSLog(@"%@", filePath);

    NSFileManager *fileManager = [NSFileManager defaultManager];

    

    [fileManager createFileAtPath:filePath contents:nil attributes:nil];

    // 创建写数据的文件句柄

    self.writeHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];

    // 获取完整的长度

    self.sumLength = response.expectedContentLength;

    

}


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

    self.currentLength += data.length;

    

    self.progress.progress = (double)self.currentLength / self.sumLength;

    

    NSLog(@"接收到的数据! ------  %ld", data.length);

    

    [self.writeHandle seekToEndOfFile];

    

    [self.writeHandle writeData:data];

    

}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection{

    NSLog(@"下载完毕");

    // 关闭连接, 不再输入数据在文件中

    [self.writeHandle closeFile];

    // 销毁

    self.writeHandle = nil;

    

    // 在下载完毕后, 对进度进行清空

    self.currentLength = 0;

    self.sumLength = 0;

    

}


- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{

    NSLog(@"error = %@", error);

}



在读取的时候只要使用NSURL的fileURLWithPath方法就可以啦.


NSString *cache = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

    

NSString *path= [NSString stringWithFormat:@"%@/man.mp4", cache];

    

NSURL *url = [NSURL fileURLWithPath:path];





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值