這篇文章主要介紹iOS如何使用AFN上傳視頻到服務器,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
在靖江等地區,都構建了全面的區域性戰略布局,加強發展的系統性、市場前瞻性、產品創新能力,以專注、極致的服務理念,為客戶提供網站設計、做網站 網站設計制作按需定制,公司網站建設,企業網站建設,品牌網站制作,成都全網營銷推廣,成都外貿網站制作,靖江網站建設費用合理。
這里介紹用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:@"轉換失敗,請重試"]; } }]; }
開始上傳
- (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:_path3Url 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]; } } }
以上是“iOS如何使用AFN上傳視頻到服務器”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注創新互聯行業資訊頻道!
本文名稱:iOS如何使用AFN上傳視頻到服務器
URL網址:http://vcdvsql.cn/article22/gjihcc.html
成都網站建設公司_創新互聯,為您提供響應式網站、定制網站、網站排名、靜態網站、ChatGPT、移動網站建設
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯