//图片压缩
NSData *data;
if (UIImagePNGRepresentation(HeadImage) == nil) {
data = UIImageJPEGRepresentation(HeadImage, 1);
} else {
data = UIImagePNGRepresentation(HeadImage);
}
CGFloat how = data.length / 1000 ;
JJLog(@"how = %fKB\n",how);
//比例
NSInteger scale = how > 500 ? 500 / how : 1.0;
//压缩图片,图片上传不能大于500kb
data = UIImageJPEGRepresentation(HeadImage, scale);
//文件流形式上传图片
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyyMMddHHmmss";
NSString *str = [formatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str];
// 上传图片,以文件流的格式
[formData appendPartWithFileData:data name:@"file" fileName:fileName mimeType:@"image/jpg"];
//以字符串,参数的形式上传图片,这个时候一半都是需要对数据进行base64编码,后台一般接受图片也要进行base64解析
NSString *imageString = [data base64EncodedStringWithOptions:0];