第五十三篇、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: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:];
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];
}
}
}
第五十三篇、OC利用AFN上传视频到服务器的更多相关文章
- 第五十四篇、OC利用AFN上传上传语音
如果不是NSData就要想办法把语音文件转化为NSData,然后才能上传服务器. 首先我们拿到语音文件对应的NSData对象 NSData *voiceData = [message valueFor ...
- 《手把手教你》系列技巧篇(五十三)-java+ selenium自动化测试-上传文件-上篇(详细教程)
1.简介 在实际工作中,我们进行web自动化的时候,文件上传是很常见的操作,例如上传用户头像,上传身份证信息等.所以宏哥打算按上传文件的分类对其进行一下讲解和分享. 2.为什么selenium没有提供 ...
- 利用 AFN 上传相册或拍照图片
概述 自定义上传图片请求,自定义调取相册及拍照,方便多处使用时调用. 详细 代码下载:http://www.demodashi.com/demo/10718.html 由于项目中多处需要上传图片,我们 ...
- 利用bootstrap上传视频文件,mvc做后台处理
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...
- WPF上传文件到服务器
利用WebClient 上传文件到服务器 创建一个空网站,创建一个UploadFile.aspx项, 服务器报500错误:检查文件保存路径是否存在,检查文件大小限制 protected void Pa ...
- 利用SecureCRT上传、下载文件(使用sz与rz命令),超实用!
利用SecureCRT上传.下载文件(使用sz与rz命令),超实用! 文章来源:http://blog.csdn.net/dongqinliuzi/article/details/39623169 借 ...
- php 利用socket上传文件
php 利用socket上传文件 张映 发表于 2010-06-02 分类目录: php 一,利用fsockopen来上传文件 以前我写过一篇关于socket通信原理的博文http://blog.51 ...
- 2.6 利用FTP上传所有文件
利用FTP上传所有文件 import os,ftptools class UploadAll(ftptools.FtpTools): #继承上一篇写的Ftptools '''upload an ent ...
- SAE利用storge上传文件 - myskies的专栏 - 博客频道 - CSDN.NET
SAE利用storge上传文件 - myskies的专栏 - 博客频道 - CSDN.NET SAE利用storge上传文件
随机推荐
- Cocos2d-x——CocosBuilder官方帮助文档翻译3 动画
Working with Animations 动画 You can use CocosBuilder for creating character animations, animating com ...
- 处理get中的中文乱码情况
1 最基本的乱码问题.这个乱码问题是最简单的乱码问题.一般新会出现.就是页面编码不一致导致的乱码.<%@ page language="java" pageEncoding= ...
- 解决用Eclipse开发Android程序时不能生成R.java的问题
今天我照着Mars老师的视频教程开始学习Android程序开发. 但是,我的Eclipse死活不能生成R.java文件,新建的工程也不行. 然后我百度,百度出来的结果一般是说这样解决: 1.clean ...
- oracle批量导出AWR报告
工作需求:项目中需要把生产库中所有的AWR报告dump出来,然后导入到方便测试的数据库中.在测试库中的AWR报告需要根据dbid和实例名逐个导出,如果遇到很多再加上RAC系统,会很麻烦.在网上找了一些 ...
- POJ 3026 Borg Maze
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7998 Accepted: 2675 Descrip ...
- Linux pipe函数
1. 函数说明 pipe(建立管道): 1) 头文件 #include<unistd.h> 2) 定义函数: int pipe(int filedes[2]); 3) 函数说明: pipe ...
- HOW MYSQL USES INTERNAL TEMPORARY TABLES
HOW MYSQL USES INTERNAL TEMPORARY TABLES Table of Contents [hide] 1)UNION queries 2)Some views 3)SQL ...
- Computer Science Theory for the Information Age-4: 一些机器学习算法的简介
一些机器学习算法的简介 本节开始,介绍<Computer Science Theory for the Information Age>一书中第六章(这里先暂时跳过第三章),主要涉及学习以 ...
- MySQL查询缓存详解
一:缓存条件,原理 MySQL Query Cache是用来缓存我们所执行的SELECT语句以及该语句的结果集,MySql在实现Query Cache的具体技术细节上类似典型的KV存储,就是将SELE ...
- HttpServer的使用
http://docs.oracle.com/javase/8/docs/jre/api/net/httpserver/spec/overview-summary.html 1.Package com ...