广告
返回顶部
首页 > 资讯 > 移动开发 >iOS实现视频下载并自动保存到相册功能
  • 493
分享到

iOS实现视频下载并自动保存到相册功能

iOS视频下载 2022-06-05 04:06:44 493人浏览 薄情痞子
摘要

iOS视频下载功能实现,并自动保存到相册(有MBProgresshUD 可以解开注释),供大家参考,具体内容如下 视频类定义属性 ///@property (nonatomic

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: iOS实现视频下载并自动保存到相册功能

本文链接: https://www.lsjlt.com/news/30397.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作