国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS上傳視頻到服務器

2019-11-09 17:07:40
字體:
來源:轉載
供稿:網友

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

    //視頻轉換為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"  mimeTypeAFURLSessionManager *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];    }}}

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

要源文件的同學可以加群聯系我:487599875,可以提供.h和.m文件。 另外博主維護一個iOS開發技術支持群:487599875,進群必須改名,群名片格式:城市-iOS-名字,例如廣州-iOS-文明。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 福清市| 池州市| 曲松县| 章丘市| 马山县| 内乡县| 合作市| 永年县| 个旧市| 胶州市| 陆丰市| 泉州市| 石棉县| 福贡县| 思南县| 灌云县| 广宗县| SHOW| 文化| 屏边| 沽源县| 丰都县| 子长县| 延长县| 庆元县| 基隆市| 新密市| 崇文区| 乐清市| 罗山县| 确山县| 松江区| 鄯善县| 定安县| 弋阳县| 苗栗县| 伊金霍洛旗| 巢湖市| 岐山县| 大方县| 江达县|