很久没发文了,最近这段时间在看前端的东西,在忙微商城webapp的事,用的vue2.0开发,打算过段时间记录总结那方面的一些知识点和坑。下面进入今天的主题。
说到iOS上传图片,网上的资料满满皆是,分为两大类。
1. 转换为 base64位
2.转换为二进制流
之前的项目里有个客诉的功能模块需要用户最多上传4张图片的需求,用的方法是第二种。在开发小程序之前,直接将多张图片用for循环将每张图片转换为二进制流再一起上传,一切都很正常。
代码如下图:
但在开发小程序版的此功能时发现,微信暂时不支持多张图片同时上传,这就让人着实蛋疼一阵。没办法,为了同时适配APP端和微信端,后台就改接口,由原来的一个接口变成了两个接口,多出来的那个接口(暂且叫B接口)为单独上传图片用的,每次只能上传一张,上传成功后返回一个关于上传图片的url,并存入到一个数组C中,而原来的接口(A接口)则不需要图片,而是将投诉的内容以及B接口返回的url产生的数组C一起加在参数字典中post上传即可了。可能到这你有点晕,下面先看下流程图吧
新建一个 imgUntil 类 ,代码如下:
imgUntil.h文件
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
typedef void (^FinishUploadAllBlock)(NSMutableArray *imgUrls);//图片上传完成回调 将服务器返回的url存到数组并将数组返回
@interface ImgUntil : NSObject
/*
param:
photosArr:由原图片数组copy过来的可变数组
desturls :图片上传成功后返回url存放用的数组并回调给controller
finish :block
controller :当前控制器
*/
+(void)synUploadAll:(NSMutableArray *)photosArr And:(NSMutableArray *)desturls finish:(FinishUploadAllBlock)finish inController:(UIViewController *)controller;
@end
imgUntil.m文件
#import "ImgUntil.h"
#import "UIView+Toast.h"
#import "AFNetworking.h"
#import "XSAPIManager.h"
#import "NetworkConstant.h"
#import "BaseModel.h"
#import "MBProgressHUD.h"
typedef void (^FinishCompressBlock)(UIImage *inFile);//图片压缩完成回调 将压缩后的图片返回
typedef void (^FinishUploadBlock)(NSString *imgUrl);//图片上传单张完成回调 将服务器返回的url存到数组并返回
@implementation ImgUntil
+(void)synUploadAll:(NSMutableArray *)photosArr And:(NSMutableArray *)desturls finish:(FinishUploadAllBlock)finish inController:(UIViewController *)controller{
if (photosArr.count>0) {
UIImage *image = photosArr[0];
[self compress:image finishCompress:^(UIImage *inFile) {//图片压缩成功回调处理
[self syncUploadImage:inFile finishUpload:^(NSString *imgUrl) {//图片上传成功回调处理
[photosArr removeObjectAtIndex:0];
[desturls addObject:imgUrl];
[self synUploadAll:photosArr And:desturls finish:finish inController:controller];//递归 直到数组中的图片全都上传完为止
} inController:controller];
}];
}else{
finish(desturls);
}
}
+(void)compress:(UIImage*)image finishCompress:(FinishCompressBlock)compress{
NSData *data= UIImageJPEGRepresentation(image,1);
NSLog(@"图片大小%lu kb",data.length/1024);
if (data.length > 1024*1024) {//判断图片是否大于1M
NSLog(@"正在压缩图片");
NSUInteger maxLength = 1024*1024;
UIImage *resultImage = image;
NSData *data = UIImageJPEGRepresentation(resultImage, 1);
NSUInteger lastDataLength = 0;
while (data.length > maxLength && data.length != lastDataLength) {
lastDataLength = data.length;
CGFloat ratio = (CGFloat)maxLength / data.length;
CGSize size = CGSizeMake((NSUInteger)(resultImage.size.width * sqrtf(ratio)),
(NSUInteger)(resultImage.size.height * sqrtf(ratio)));
UIGraphicsBeginImageContext(size);
[resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
data = UIImageJPEGRepresentation(resultImage, 1);
NSLog(@"压缩后图片大小%lu kb",data.length/1024);
}
compress(resultImage);
}else{
compress(image);
}
}
+(void)syncUploadImage:(UIImage*)image finishUpload:(FinishUploadBlock)upload inController:(UIViewController *)controller{
[MBProgressHUD showHUDAddedTo:controller.view animated:YES];
NSLog(@"正在上传图片");
NSString *url = [NSString stringWithFormat:@"%@/images/uploadImgFile",host];
[[XSAPIManager manager] MemberPOST:url requestType:XSRequestTypeJSON parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSData *data= UIImageJPEGRepresentation(image,1);
[formData appendPartWithFileData:data name:@"file" fileName:@"iosfile.jpg" mimeType:@"image/*"];
} success:^(id responseObject) {
NSLog(@"返回的url %@",responseObject);
BaseModel *model = [BaseModel mj_objectWithKeyValues:responseObject];
if (model.code==1) {
upload(responseObject[@"data"]);
}
[MBProgressHUD hideHUDForView:controller.view animated:YES];
} failure:^(NSError *error) {
[MBProgressHUD hideHUDForView:controller.view animated:YES];
} ];
}
@end
在controller中调用.h文件中的类方法即可。
以上!