iOS实现视频下载并自动保存到相册功能
程序员文章站
2022-05-25 22:55:05
ios视频下载功能实现,并自动保存到相册(有mbprogresshud 可以解开注释),供大家参考,具体内容如下视频类定义属性///@property (nonatomic,strong) mbpro...
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实现图片抖动效果