https://blog.csdn.net/guofengios/article/details/52945028 //原文
gif 要经过特殊处理 传入内容直接就nsddata类型
其他图片就uiimage 类型
suffixStr //后缀名
if ([[suffixStr uppercaseString] isEqualToString:@"GIF"]) {
PHImageRequestOptions *options = [PHImageRequestOptions new];
options.resizeMode = PHImageRequestOptionsResizeModeFast;
options.synchronous = YES;
PHCachingImageManager *imageManager = [[PHCachingImageManager alloc] init];
[imageManager requestImageDataForAsset:model.asset options:options resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
@strongify(self);
if ([dataUTI isEqualToString:(__bridge NSString *)kUTTypeGIF]) {
//gif提交
}
}];
}else {
//一般图片提交
}
//上传图片
-(void)uploadVideoOrPhotoFileForUrl:(NSString *)url
param:(NSDictionary *)param
file:(id)files
fileName:(NSString *)fileName
success:(HttpRequestSuccessBlockHandler)successBlockHandler
fail:(HttpRequestFailBlockHandler)failBlockHandler
{
[self POST:url parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
if([files isKindOfClass:[UIImage class]] || [files isKindOfClass:[NSData class]]){
NSString *extension = [fileName componentsSeparatedByString:@"."].lastObject;
NSData *imageData = nil;
if ([[extension uppercaseString] isEqualToString:@"GIF"]) {
imageData = files;
}else {
imageData = UIImageJPEGRepresentation(files,0.3);
}
[formData appendPartWithFileData:imageData name:fileName fileName:fileName mimeType:[NSString stringWithFormat:@"image/%@", extension]];
}else {
//获取文件的后缀名
NSString *extension = [fileName componentsSeparatedByString:@"."].lastObject;
//设置mimeType
NSString *mimeType;
if ([fileName isContainsString:@"image"]) {
mimeType = [NSString stringWithFormat:@"image/%@", extension];
} else {
mimeType = [NSString stringWithFormat:@"video/%@", extension];
}
NSString *name = [[fileName componentsSeparatedByString:@"."] firstObject];
NSError *error;
[formData appendPartWithFileURL:files name:name fileName:fileName mimeType:mimeType error:&error];
if (error) {
NSLog(@"appendPartWithFileURL error: %@", error);
}
}
} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if(responseObject != nil){
if(successBlockHandler){
successBlockHandler(responseObject);
}
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
#if DEBUG
NSLog(@"http requst error %@",[error description]);
#endif
if(error.code == NSURLErrorNotConnectedToInternet){
[SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeNone];
[SVProgressHUD showImage:nil status:DefaultNetError];
}
if(failBlockHandler){
failBlockHandler(error);
}
}];
}