@interface NSString (Extension)
/**
* 将image进行base64编码
*/
+ (NSString *)imageBase64:(UIImage *)image;
@end
+ (NSString *)imageBase64:(UIImage *)image
{
image = [self imageWithImageSimple:image scaledToSize:CGSizeMake(200, 200)];
NSData *data =[NSData data];
if (UIImagePNGRepresentation(image)) {
//返回为png图像。
data = UIImagePNGRepresentation(image);
}else {
//返回为JPEG图像。
data = UIImageJPEGRepresentation(image, 0.5);
}
NSString *base64ImageStr = [data base64EncodedStringWithOptions:0];
base64ImageStr = (__bridge NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)base64ImageStr, NULL,
CFSTR("="), kCFStringEncodingUTF8);
return base64ImageStr;
}
/**
* 调整发图片大小
*/
+ (UIImage *)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}