前言:
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;
}
两种方法均可公用不在重复说明