iOS實(shí)現(xiàn)視頻下載并自動保存到相冊功能
iOS視頻下載功能實(shí)現(xiàn),并自動保存到相冊(有MBProgressHUD 可以解開注釋),供大家參考,具體內(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];
}
///通過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"];
//拼接文件絕對路徑
NSString *path = [documents stringByAppendingPathComponent:response.suggestedFilename];
//視頻存放到這個位置
[fileManger moveItemAtURL:location toURL:[NSURL fileURLWithPath:path] error:nil];
///保存到相冊
UISaveVideoAtPathToSavedPhotosAlbum(path, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
}];
///開始下載任務(wù)
[task resume];
}
//保存視頻完成之后的回調(diào)
- (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];
}
// 進(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]];
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IOS 開發(fā)之讀取addressbook的實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了IOS 開發(fā)之讀取addressbook的實(shí)現(xiàn)實(shí)例的相關(guān)資料,希望通過本文大家能夠掌握這樣的內(nèi)容,需要的朋友可以參考下2017-09-09
設(shè)計(jì)模式中的迭代器模式在Cocoa Touch框架中的使用
這篇文章主要介紹了設(shè)計(jì)模式中的迭代器模式在Cocoa Touch框架中的使用,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03
iOS開發(fā)教程之識別圖片中二維碼功能的實(shí)現(xiàn)
長按識別二維碼這個功能相信對大家來說都不陌生,最近工作中就遇到了這個需求,所以下面這篇文章主要給大家介紹了關(guān)于利用iOS識別圖片中二維碼的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-07-07
iOS開發(fā)網(wǎng)絡(luò)編程之?dāng)帱c(diǎn)續(xù)傳
在下載較大的文件的時候,一次不能下載完畢,這就需要用到斷點(diǎn)續(xù)傳,那么在IOS開發(fā)中該如何實(shí)現(xiàn)呢,下面跟著小編一起通過本文來學(xué)習(xí)下。2016-08-08
iOS 監(jiān)聽回調(diào)機(jī)制KVO實(shí)例
下面小編就為大家分享一篇iOS 監(jiān)聽回調(diào)機(jī)制KVO實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
ios動態(tài)設(shè)置lbl文字標(biāo)簽的高度
本文給大家分享的是ios動態(tài)設(shè)置lbl文字標(biāo)簽的高度寬度的方法,一共給大家匯總了3種方法,小伙伴們根據(jù)自己的項(xiàng)目需求自由選擇。2015-05-05

