IOS中UIImage的数据量压缩有两种方式,一种是图片尺寸不变,降低图片分辨率,代码方法为:
//1.0为压缩系数,介于0~1之间。压缩系数越小,会大大降低图片清晰度
NSData *data = UIImageJPEGRepresentation(originalImage, 1.0);
另一种是按比例减小图片的尺寸,从而减小图片内存
按比例缩小图片尺寸的代码
-(UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize { UIGraphicsBeginImageContext(CGSizeMake(image.size.width*scaleSize,image.size.height*scaleSize)); [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height *scaleSize)]; UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return scaledImage; }