iOS上传图片和视频(base64和file)
前言:
ios开发中经常会使用到图片和视频上传及保存到相册,下面我讲介绍视频图片的两种上传服务器的方法。以阿里云的oss服务器为例。
友情提示:
上传图片方法在app中使用很广泛,最好单独写一个图片上传的类,这样就很方便了。
base64上传图片:
nsstring *strtype = [gxtoolclass getbase64str:image];
nsmutabledictionary *dic = [nsmutabledictionary dictionarywithcapacity:1];
[dic setvalue:strtype forkey:@"base64"];
afhttpsessionmanager *manager = [afhttpsessionmanager manager];
[manager post:[base_host stringbyappendingstring:http_global_oneimage] parameters:dic progress:^(nsprogress * _nonnull uploadprogress) {
} success:^(nsurlsessiondatatask * _nonnull task, id _nullable responseobject) {
if ([responseobject[getcode] intvalue] == 0) {
nsdictionary *dic = responseobject[getdata];
nsstring *file = dic[@"file"];
if (self.block) {
self.block(file);
}
}else{
[[appdelegate getapp] showalert:responseobject[getnewmess] type:alertviewtype_toast];
if (self.otherblock) {
self.otherblock(responseobject[getnewmess]);
}
}
} failure:^(nsurlsessiondatatask * _nullable task, nserror * _nonnull error) {
if (self.failblock) {
[[appdelegate getapp] showalert:@"上传失败请稍后重试" type:alertviewtype_toast];
self.failblock(@"上传失败请稍后重试");
}
}];
+(nsstring *)getbase64str:(uiimage *)ima //图片转成base64的 方法
{
nsdata *data = uiimagejpegrepresentation(ima, 0.3);
nsstring *imagestyle = [self getimagestyle:data];
nsstring *str = [data base64encodedstringwithoptions:nsdatabase64encoding64characterlinelength];
str = [nsstring stringwithformat:@"data:image/%@;base64,%@", imagestyle, str];
return str;
}
file视频上传:
afhttpsessionmanager *manager = [afhttpsessionmanager manager];
manager.requestserializer = [self gethttpheader];
// manager.responseserializer.acceptablecontenttypes = nil;
manager.responseserializer.acceptablecontenttypes = [nsset setwithobjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
[self beginloading:@"上传中" view:self.view];
[manager post:[base_host stringbyappendingstring:http_main_videoupload] parameters:dic constructingbodywithblock:^(id<afmultipartformdata> _nonnull formdata) {
nsstring *strimagestyle = [gxtoolclass getimagestyle:dataima];
[formdata appendpartwithfiledata:data1 name:@"file" filename:_outputpath mimetype:@"mov"];
[formdata appendpartwithfiledata:dataima name:@"thumb" filename:[nsstring stringwithformat:@"file.%@", strimagestyle] mimetype:strimagestyle];
} progress:^(nsprogress * _nonnull uploadprogress) {
} success:^(nsurlsessiondatatask * _nonnull task, id _nullable responseobject) {
[self stoploading];
if ([responseobject[getcode] intvalue] == 0) {
[self.navigationcontroller popviewcontrolleranimated:yes];
}
[self showalert:responseobject[getmessage] type:alertviewtype_toast];
} failure:^(nsurlsessiondatatask * _nullable task, nserror * _nonnull error) {
[self stoploading];
[self showalert:@"上传失败请稍后重试" type:alertviewtype_toast];
}];
+(nsstring *)getimagestyle:(nsdata *)data//获取图片格式
{
uint8_t c;
[data getbytes:&c length:1];
switch (c) {
case 0xff:
return @"jpeg";
case 0x89:
return @"png";
case 0x47:
return @"gif";
case 0x49:
case 0x4d:
return @"tiff";
case 0x52:
if ([data length] < 12) {
return nil;
}
nsstring *teststring = [[nsstring alloc] initwithdata:[data subdatawithrange:nsmakerange(0, 12)] encoding:nsasciistringencoding];
if ([teststring hasprefix:@"riff"] && [teststring hassuffix:@"webp"]) {
return @"webp";
}
return nil;
}
return nil;
}
两种方法均可公用不在重复说明
上一篇: 独立站做网红营销需要具备哪些要素
下一篇: 网红营销 最怕摊上什么事儿?
推荐阅读
-
iOS上传图片和视频(base64和file)
-
base64和图片的互转(HTML5的File实现)
-
Thinkphp5框架实现图片、音频和视频文件的上传功能详解
-
php 视频、音频和图片文件上传,该如何解决
-
iOS 上传图片视图【支持删除和添加】(使用UICollectionViewCell、UITableViewCell 及自动布局Masonry 框架。采用MVVM实现。)
-
移动端图片上传方法【更好的兼容安卓IOS和微信】
-
给上传的文件生成缩略图(图片和视频)
-
iOS上传图片和视频(base64和file)
-
修复dtcms5.0后台管理编辑器上传视频和图片被过滤问题
-
type=file多图片上传 预览和删除