iOS如何实现视频下载并自动保存到相册功能
小编给大家分享一下iOS如何实现视频下载并自动保存到相册功能,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
创新互联公司专注于企业营销型网站建设、网站重做改版、大名网站定制设计、自适应品牌网站建设、H5技术、商城开发、集团公司官网建设、外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为大名等各大城市提供网站开发制作服务。
iOS视频下载功能实现,并自动保存到相册(有MBProgressHUD 可以解开注释),供大家参考,具体内容如下
视频类定义属性
///@property (nonatomic,strong) MBProgressHUD *hud; @property (nonatomic,strong) NSURLSession *session; ///视频播放和下载用的url @property (nonatomic,strong) NSURL *url;
///初始化session - (NSURLSession *)session{ if(_session == nil) { NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; _session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil]; } return _session; } ///下载 - (void)download:(UIBarButtonItem *)btnItem{ ///初始化Session _session = [XMConciseVedioPlayer getSession:_session]; ///self.hud = [MBProgressHUD showHUDAddedTo:self animated:YES]; [self downloadFileWithUrl:self.url]; } ///通过url下载 - (void)downloadFileWithUrl:(NSURL *)url{ NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:1.0 timeoutInterval:5.0]; ///下载任务 [[self.session downloadTaskWithRequest:request]resume]; NSURLSessionDownloadTask *task = [_session downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) { ///[self.hud setLabelText:[NSString stringWithFormat:@"下载成功"]]; NSFileManager *fileManger = [NSFileManager defaultManager]; ///沙盒Documents路径 NSString *documents = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; //拼接文件绝对路径 NSString *path = [documents stringByAppendingPathComponent:response.suggestedFilename]; //视频存放到这个位置 [fileManger moveItemAtURL:location toURL:[NSURL fileURLWithPath:path] error:nil]; ///保存到相册 UISaveVideoAtPathToSavedPhotosAlbum(path, self, @selector(video:didFinishSavingWithError:contextInfo:), nil); }]; ///开始下载任务 [task resume]; } //保存视频完成之后的回调 - (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo { if (!error) { ///[self.hud setLabelText:[NSString stringWithFormat:@"保存到相册成功"]]; } else { ///[self.hud setLabelText:[NSString stringWithFormat:@"下载失败"]]; } ///[self.hud hide:YES afterDelay:3.0]; } // 进度数据 - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{ float progress = (float)totalBytesWritten / totalBytesExpectedToWrite; long pro = (long)(progress *100); ///[self.hud setLabelText:[NSString stringWithFormat:@"下载进度:%ld%%",pro]]; }
看完了这篇文章,相信你对“iOS如何实现视频下载并自动保存到相册功能”有了一定的了解,如果想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!
当前题目:iOS如何实现视频下载并自动保存到相册功能
URL链接:http://azwzsj.com/article/jjgjec.html