iOS上傳視頻到服務(wù)器題

iOS上傳視頻到服務(wù)器

這里介紹用AFN上傳本地拍攝的視頻到服務(wù)端

//視頻轉(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)換失敗,請重試"];

}

}];

}

#pragma mark - 開始上傳

-(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 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];

}

記得上傳成功之后要刪除沙盒中的視頻文件哦屎债!代碼奉上

#pragma mark - 清除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];

}

}

}

好汗唱,整體思路已經(jīng)清楚,拿到視頻資源惶我,先轉(zhuǎn)為mp4承耿,寫進沙盒冠骄,然后上傳,上傳成功后要記得刪除沙盒中的文件加袋。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末凛辣,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子职烧,更是在濱河造成了極大的恐慌扁誓,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,919評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蚀之,死亡現(xiàn)場離奇詭異蝗敢,居然都是意外死亡,警方通過查閱死者的電腦和手機足删,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,567評論 3 392
  • 文/潘曉璐 我一進店門寿谴,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人失受,你說我怎么就攤上這事讶泰∮缴” “怎么了?”我有些...
    開封第一講書人閱讀 163,316評論 0 353
  • 文/不壞的土叔 我叫張陵痪署,是天一觀的道長码泞。 經(jīng)常有香客問我,道長狼犯,這世上最難降的妖魔是什么余寥? 我笑而不...
    開封第一講書人閱讀 58,294評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮辜王,結(jié)果婚禮上劈狐,老公的妹妹穿的比我還像新娘。我一直安慰自己呐馆,他們只是感情好肥缔,可當我...
    茶點故事閱讀 67,318評論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著汹来,像睡著了一般续膳。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上收班,一...
    開封第一講書人閱讀 51,245評論 1 299
  • 那天坟岔,我揣著相機與錄音,去河邊找鬼摔桦。 笑死社付,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的邻耕。 我是一名探鬼主播鸥咖,決...
    沈念sama閱讀 40,120評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼兄世!你這毒婦竟也來了啼辣?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,964評論 0 275
  • 序言:老撾萬榮一對情侶失蹤御滩,失蹤者是張志新(化名)和其女友劉穎鸥拧,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體削解,經(jīng)...
    沈念sama閱讀 45,376評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡富弦,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,592評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了氛驮。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片腕柜。...
    茶點故事閱讀 39,764評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出媳握,到底是詐尸還是另有隱情,我是刑警寧澤磷脯,帶...
    沈念sama閱讀 35,460評論 5 344
  • 正文 年R本政府宣布蛾找,位于F島的核電站,受9級特大地震影響赵誓,放射性物質(zhì)發(fā)生泄漏打毛。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,070評論 3 327
  • 文/蒙蒙 一俩功、第九天 我趴在偏房一處隱蔽的房頂上張望幻枉。 院中可真熱鬧,春花似錦诡蜓、人聲如沸熬甫。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,697評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽椿肩。三九已至,卻和暖如春豺谈,著一層夾襖步出監(jiān)牢的瞬間郑象,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,846評論 1 269
  • 我被黑心中介騙來泰國打工茬末, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留厂榛,地道東北人。 一個月前我還...
    沈念sama閱讀 47,819評論 2 370
  • 正文 我出身青樓丽惭,卻偏偏與公主長得像击奶,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子吐根,可洞房花燭夜當晚...
    茶點故事閱讀 44,665評論 2 354

推薦閱讀更多精彩內(nèi)容