第五十三篇、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上传视频到服务器相关推荐
- 利用 AFN 上传相册或拍照图片
概述 自定义上传图片请求,自定义调取相册及拍照,方便多处使用时调用. 详细 代码下载:http://www.demodashi.com/demo/10718.html 由于项目中多处需要上传图片,我们 ...
- jmeter 图片上传不上去_松勤软件性能测试-教你如何利用jmeter上传视频图片附件...
原标题:松勤软件性能测试-教你如何利用jmeter上传视频图片附件 http上传附件一般用的Content-Type: multipart/form-data;文中是先通过fiddler抓取手机端的请 ...
- iOS上传视频到服务器
iOS上传视频到服务器 这里介绍用AFN上传本地拍摄的视频到服务端 //视频转换为MP4//转码操作..._hud.mode = MBProgressHUDModeIndeterminate;_hud ...
- 微信开发上传视频到服务器,微信小程序中实现上传视频的开发代码
微信小程序中实现上传视频的开发代码,视频,接口,上传,代码,服务器 微信小程序中实现上传视频的开发代码 易采站长站,站长之家为您整理了微信小程序中实现上传视频的开发代码的相关内容. 本篇文章给大家带来 ...
- php利用ftp上传视频,PHP用FTP类上传文件视频等的简单实现方法
文件1:ftp.php<?php /** * 仿写CodeIgniter的FTP类 * FTP基本操作: * 1) 登陆; connect * 2) 当前目录文件列表; filelist * 3 ...
- 苹果手机利用Itunes上传视频等文件
1.将手机通过链接线连接电脑. 2.在Itunes左上角,点击3.点击出现的文件共享4.在右侧出现的app里,任选一个.再在最右侧,点击添加文件.出现选择文件框后,选择文件,即可将电脑上的文件导入手机 ...
- 微信小程序上传视频到服务器太慢,微信小程序视频播放加载很慢
- 当前 Bug 的表现(可附上截图) - 预期表现 - 复现路径 - 提供一个最简复现 Demo 微信小程序视频播放加载很慢,一直转圈,3分钟后才能播放,而且播放很卡,加了带宽和cdn也没有效果,官 ...
- jsch 移动服务器上文件,jsch上传文件到服务器
需求就是上传文件到服务器,服务器的存储地址由程序决定然后可以自动创建. 使用第三方:jsch JSch 是SSH2的一个纯Java实现.它允许你连接到一个sshd 服务器,使用端口转发,X11转发,文 ...
- sftp上传文件服务器,使用SFTP上传文件到服务器的简单使用
最近用到SFTP上传文件查找了一些资料后自己做了一点总结,方便以后的查询 /** * 将文件上传到服务器 * * @param filePath * 文件路径 * @param channelSftp ...
最新文章
- Python特殊语法:filter、map、reduce、lambda [转]
- php echarts 两条曲线图,使用laravel和ECharts实现折线图效果的例子
- 前端学习(1613):oracle基本概念
- vue-router组件重用 路由切换时的问题
- 递归法 最大公倍数 斐波那契数列 数组插入排序改递归
- C# 性能优化之斤斤计较篇 二
- 数论 之 筛法总结(艾托拉斯特尼筛法+欧拉筛法)
- echarts实现半圆饼图
- 5+API实现微信分享功能
- 工作流程管理的重要性
- xp隐藏桌面计算机图标不见了怎么办,XP系统桌面IE图标不见了怎么办?IE图标消失了怎么恢复?...
- Python脚本批量转换图片格式至ico
- 【githubboy】一项视频自动抠图技术,强大、实时、高分辨率的人物视频抠图方法
- 微信小程序开发工具取消用户授权
- 把数组里的字符串转换成数字或者把数字转换成字符串
- 视觉SLAM前端——PnP
- 区块链创业者惠轶之死
- C#Base64简单加密与解密
- 40岁挣够4000万的N种可能
- JAVA毕业设计BS景区票务管理系统设计与实现计算机源码+lw文档+系统+调试部署+数据库