1、图片image转base64
-(NSString *)imageChangeBase64: (UIImage *)image{
NSData *imageData = nil;
//NSString *mimeType = nil;
if ([self imageHasAlpha:image]) {
imageData = UIImagePNGRepresentation(image);
//mimeType = @"image/png";
}else{
imageData = UIImageJPEGRepresentation(image, 0.3f);
//mimeType = @"image/jpeg";
}
return [NSString stringWithFormat:@"%@",[imageData base64EncodedStringWithOptions: 0]];
}
-(BOOL)imageHasAlpha:(UIImage *)image{
CGImageAlphaInfo alpha = CGImageGetAlphaInfo(image.CGImage);
return (alpha == kCGImageAlphaFirst ||
alpha == kCGImageAlphaLast ||
alpha == kCGImageAlphaPremultipliedFirst ||
alpha == kCGImageAlphaPremultipliedLast);
}
注意::上传的base64字符串 前加文件格式[NSString stringWithFormat:@"png@%@",strImage];