iOS 使用AFN上传视频到服务器示例代码
程序员文章站
2024-02-08 15:11:28
这里介绍用afn上传本地拍摄的视频到服务端。
整体思路:拿到视频资源,先转为mp4,写进沙盒,然后上传,上传成功后要记得删除沙盒中的文件。
压缩导出视频...
这里介绍用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:_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]; }
清除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上传视频到服务器示例代码
-
[PHP]利用XAMPP搭建本地服务器, 然后利用iOS客户端上传数据到本地服务器中(三. PHP端代码实现),xamppios_PHP教程
-
[PHP]利用XAMPP搭建本地服务器, 然后利用iOS客户端上传数据到本地服务器中(三. PHP端代码实现) - M_Lee
-
ios用AFN进行文件上传的示例代码
-
iOS视频压缩存储至本地并上传至服务器实例代码
-
iOS 使用AFN上传视频到服务器示例代码
-
iOS使用AFN进行单图和多图上传的实例代码
-
[PHP]利用XAMPP搭建本地服务器, 然后利用iOS客户端上传数据到本地服务器中三 PHP端代码实现
-
[PHP]利用XAMPP搭建本地服务器, 然后利用iOS客户端上传数据到本地服务器中(三. PHP端代码实现) - M_Lee
-
[PHP]利用XAMPP搭建本地服务器, 然后利用iOS客户端上传数据到本地服务器中(三. PHP端代码实现)