日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

第五十三篇、OC利用AFN上传视频到服务器

發布時間:2025/4/14 编程问答 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第五十三篇、OC利用AFN上传视频到服务器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

整體思路已經清楚,拿到視頻資源,先轉為mp4,寫進沙盒,然后上傳,上傳成功后刪除沙盒中的文件。

本地拍攝的視頻,上傳到服務器:

//視頻轉換為MP4//轉碼操作..._hud.mode = MBProgressHUDModeIndeterminate;_hud.labelText = @"轉碼中...";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---轉換成功");_filePath = _outfilePath;_filePathURL = [NSURL URLWithString:[NSString stringWithFormat:@"file://%@",_outfilePath]];MyLog(@"轉換完成_filePath = %@\n_filePathURL = %@",_filePath,_filePathURL);//獲取大小和長度 [self SetViewText];[self uploadNetWorkWithParam:@{@"contenttype":@"application/octet-stream",@"discription":description}];}else{MyLog(@"轉換失敗,值為:%li,可能的原因:%@",(long)[exportSession status],[[exportSession error] localizedDescription]);[_hud hide:YES];[MyHelper showAlertWith:nil txt:@"轉換失敗,請重試"];}}];}#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<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:selfforKeyPath:@"fractionCompleted"context:NULL]; }]; [progress addObserver:selfforKeyPath:@"fractionCompleted"options:NSKeyValueObservingOptionNewcontext: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];} } }

?

轉載于:https://www.cnblogs.com/HJQ2016/p/5962813.html

總結

以上是生活随笔為你收集整理的第五十三篇、OC利用AFN上传视频到服务器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。