iOS 使用AFN上傳視頻到服務(wù)器示例代碼
更新時間:2017年04月17日 15:16:09 作者:Mint丶bin
本篇文章主要介紹了iOS 使用AFN上傳視頻到服務(wù)器示例代碼,具有一定的參考價值,有興趣的可以了解一下。
這里介紹用AFN上傳本地拍攝的視頻到服務(wù)端。
整體思路:拿到視頻資源,先轉(zhuǎn)為mp4,寫進(jìn)沙盒,然后上傳,上傳成功后要記得刪除沙盒中的文件。
壓縮導(dǎo)出視頻
//視頻轉(zhuǎn)換為MP4 //轉(zhuǎn)碼操作... _hud.mode = MBProgressHUDModeIndeterminate; _hud.labelText = @"轉(zhuǎn)碼中..."; AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:_filePathURL options:nil]; NSDateFormatter* formater = [[NSDateFormatter alloc] init]; [formater setDateFormat:@"yyyyMMddHHmmss"]; _fileName = [NSString stringWithFormat:@"output-%@.mp4",[formater stringFromDate:[NSDate date]]]; _outfilePath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/%@", _fileName]; NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset]; if ([compatiblePresets containsObject:AVAssetExportPresetMediumQuality]) { MyLog(@"outPath = %@",_outfilePath); AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:avAsset presetName:AVAssetExportPresetMediumQuality]; exportSession.outputURL = [NSURL fileURLWithPath:_outfilePath]; exportSession.outputFileType = AVFileTypeMPEG4; [exportSession exportAsynchronouslyWithCompletionHandler:^{ if ([exportSession status] == AVAssetExportSessionStatusCompleted) { MyLog(@"AVAssetExportSessionStatusCompleted---轉(zhuǎn)換成功"); _filePath = _outfilePath; _filePathURL = [NSURL URLWithString:[NSString stringWithFormat:@"file://%@",_outfilePath]]; MyLog(@"轉(zhuǎn)換完成_filePath = %@\\n_filePathURL = %@",_filePath,_filePathURL); //獲取大小和長度 [self SetViewText]; [self uploadNetWorkWithParam:@{@"contenttype":@"application/octet-stream",@"discription":description}]; }else{ MyLog(@"轉(zhuǎn)換失敗,值為:%li,可能的原因:%@",(long)[exportSession status],[[exportSession error] localizedDescription]); [_hud hide:YES]; [MyHelper showAlertWith:nil txt:@"轉(zhuǎn)換失敗,請重試"]; } }]; }
開始上傳
- (void)uploadNetWorkWithParam:(NSDictionary*)dict { MyLog(@"開始上傳_filePath = %@\\n_filePathURL = %@",_filePath,_filePathURL); AFHTTPRequestSerializer *ser=[[AFHTTPRequestSerializer alloc]init]; NSMutableURLRequest *request = [ser multipartFormRequestWithMethod:@"POST" URLString:[NSString stringWithFormat:@"%@%@",kBaseUrl,kVideoUploadUrl] parameters:@{@"path":@"show",@"key":_key,@"discription":dict[@"discription"],@"isimage":@(_isImage)} constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { [formData appendPartWithFileURL:_filePathURL name:@"file" fileName:_fileName mimeType:dict[@"contenttype"] error:nil]; if (!_isImage) { [formData appendPartWithFileURL:_path2Url name:@"tmp" fileName:@"tmp.PNG" mimeType:@"image/png" error:nil]; } } error:nil]; //@"image/png" @"application/octet-stream" mimeType AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; NSProgress *progress = nil; NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { if (error) { MyLog(@"request = %@", request ); MyLog(@"response = %@", response ); MyLog(@"Error: %@", error ); [_hud hide:YES]; CXAlertView *alert=[[CXAlertView alloc]initWithTitle:NSLocalizedString(@"Warning", nil) message:NSLocalizedString(@"Upload Failed",nil) cancelButtonTitle:NSLocalizedString(@"Iknow", nil)]; alert.showBlurBackground = NO; [alert show]; } else { MyLog(@"%@ %@", response, responseObject); NSDictionary *backDict=(NSDictionary *)responseObject; if ([backDict[@"success"] boolValue] != NO) { _hud.labelText = NSLocalizedString(@"Updating", nil); [self UpdateResxDateWithDict:backDict discription:dict[@"discription"]]; [_hud hide:YES]; }else{ [_hud hide:YES]; [MyHelper showAlertWith:nil txt:backDict[@"msg"]]; } } [progress removeObserver:self forKeyPath:@"fractionCompleted" context:NULL]; }]; [progress addObserver:self forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew context:NULL]; [progress setUserInfoObject:@"someThing" forKey:@"Y.X."]; [uploadTask resume]; }
清除documents中視頻
-(void)ClearMovieFromDoucments{ NSFileManager *fileManager = [NSFileManager defaultManager]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSArray *contents = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:NULL]; NSEnumerator *e = [contents objectEnumerator]; NSString *filename; while ((filename = [e nextObject])) { MyLog(@"%@",filename); if ([filename isEqualToString:@"tmp.PNG"]) { MyLog(@"刪除%@",filename); [fileManager removeItemAtPath:[documentsDirectory stringByAppendingPathComponent:filename] error:NULL]; continue; } if ([[[filename pathExtension] lowercaseString] isEqualToString:@"mp4"]|| [[[filename pathExtension] lowercaseString] isEqualToString:@"mov"]|| [[[filename pathExtension] lowercaseString] isEqualToString:@"png"]) { MyLog(@"刪除%@",filename); [fileManager removeItemAtPath:[documentsDirectory stringByAppendingPathComponent:filename] error:NULL]; } } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- IOS-MVC層讀取服務(wù)器接口JSON數(shù)據(jù)
- ios 服務(wù)器端推送證書生成的方法
- iOS APP 多服務(wù)器環(huán)境分離的方法
- iOS將相冊中圖片上傳至服務(wù)器的方法
- 詳解Nginx服務(wù)器和iOS的HTTPS安全通信
- Java使用NioSocket手動實(shí)現(xiàn)HTTP服務(wù)器
- iOS視頻壓縮存儲至本地并上傳至服務(wù)器實(shí)例代碼
- IOS用AFN發(fā)送字符串形式的Json數(shù)據(jù)給服務(wù)器實(shí)例
- IOS開發(fā)教程之put上傳文件的服務(wù)器的配置及實(shí)例分享
- IOS利用CocoaHttpServer搭建手機(jī)本地服務(wù)器
相關(guān)文章
iOS中關(guān)于音樂鎖屏控制音樂(鎖屏信息設(shè)置)的實(shí)例代碼
這篇文章主要介紹了 iOS中關(guān)于音樂鎖屏控制音樂(鎖屏信息設(shè)置)的實(shí)例代碼,需要的朋友可以參考下2017-01-01深入了解iOS開發(fā)中UIWindow的相關(guān)使用
這篇文章主要介紹了深入了解iOS開發(fā)中UIWindow的相關(guān)使用,以及iOS8以后產(chǎn)生的相關(guān)變化,需要的朋友可以參考下2015-10-10iOS如何優(yōu)雅地消除應(yīng)用角標(biāo)詳解
關(guān)于應(yīng)用角標(biāo)相信大家應(yīng)該都有所了解吧,這篇文章主要給大家介紹了關(guān)于iOS如何優(yōu)雅地消除應(yīng)用角標(biāo)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位iOS開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12iOS Runntime 動態(tài)添加類方法并調(diào)用-class_addMethod
這篇文章主要介紹了iOS Runntime 動態(tài)添加類方法并調(diào)用-class_addMethod的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-08-08