当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS实现视频下载并自动保存到相册功能

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

2020年05月14日  | 移动技术网IT编程  | 我要评论

飞机失踪35年,侯海洋,陈公培

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

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网