网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

iOS如何实现视频下载并自动保存到相册功能-创新互联

小编给大家分享一下iOS如何实现视频下载并自动保存到相册功能,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

创新互联建站坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站建设、成都网站制作、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的洪雅网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

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如何实现视频下载并自动保存到相册功能”有了一定的了解,如果想了解更多相关知识,欢迎关注创新互联网站建设公司行业资讯频道,感谢各位的阅读!

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文标题:iOS如何实现视频下载并自动保存到相册功能-创新互联
本文路径:http://bjjierui.cn/article/ddjeop.html

其他资讯