欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS實(shí)現(xiàn)視頻下載并自動(dòng)保存到相冊(cè)功能

 更新時(shí)間:2020年02月21日 11:13:23   作者:zyw658000  
這篇文章主要為大家詳細(xì)介紹了ios 視頻下載功能實(shí)現(xiàn),并自動(dòng)保存到相冊(cè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

iOS視頻下載功能實(shí)現(xiàn),并自動(dòng)保存到相冊(cè)(有MBProgressHUD 可以解開(kāi)注釋),供大家參考,具體內(nèi)容如下

視頻類定義屬性

///@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];
 
}
///通過(guò)url下載
- (void)downloadFileWithUrl:(NSURL *)url{
 NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:1.0 timeoutInterval:5.0];
 ///下載任務(wù)
 [[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"];
 //拼接文件絕對(duì)路徑
 NSString *path = [documents stringByAppendingPathComponent:response.suggestedFilename];
 //視頻存放到這個(gè)位置
 [fileManger moveItemAtURL:location toURL:[NSURL fileURLWithPath:path] error:nil];
 ///保存到相冊(cè)
 UISaveVideoAtPathToSavedPhotosAlbum(path, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);

 
 }];
 ///開(kāi)始下載任務(wù)
 [task resume];
 
}

//保存視頻完成之后的回調(diào)
- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
 if (!error) {
 ///[self.hud setLabelText:[NSString stringWithFormat:@"保存到相冊(cè)成功"]];
 } else {
 ///[self.hud setLabelText:[NSString stringWithFormat:@"下載失敗"]];
 }
 ///[self.hud hide:YES afterDelay:3.0];
}


// 進(jìn)度數(shù)據(jù)
- (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:@"下載進(jìn)度:%ld%%",pro]];
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論