小編給大家分享一下iOS如何實(shí)現(xiàn)視頻下載并自動(dòng)保存到相冊(cè)功能,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
創(chuàng)新互聯(lián)公司專(zhuān)注于企業(yè)營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、網(wǎng)站重做改版、大名網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5技術(shù)、商城開(kāi)發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性?xún)r(jià)比高,為大名等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
iOS視頻下載功能實(shí)現(xiàn),并自動(dòng)保存到相冊(cè)(有MBProgressHUD 可以解開(kāi)注釋),供大家參考,具體內(nèi)容如下
視頻類(lè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]]; }
看完了這篇文章,相信你對(duì)“iOS如何實(shí)現(xiàn)視頻下載并自動(dòng)保存到相冊(cè)功能”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
新聞名稱(chēng):iOS如何實(shí)現(xiàn)視頻下載并自動(dòng)保存到相冊(cè)功能
鏈接分享:http://aaarwkj.com/article42/jjgjec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、標(biāo)簽優(yōu)化、網(wǎng)站設(shè)計(jì)公司、外貿(mào)網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
移動(dòng)網(wǎng)站建設(shè)知識(shí)