图片不失真压缩
//对图片尺寸进行压缩
-(UIImage *)compressOriginalImage:(UIImage *)image toSize:(CGSize)size{
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
//对图片质量进行压缩
- (NSData *)imageData:(UIImage *)myimage
{
NSData *data=UIImageJPEGRepresentation(myimage, 1.0);
if (data.length>100*1024) {
if (data.length>1024*1024) {//1M以及以上
data=UIImageJPEGRepresentation(myimage, 0.1);
}else if (data.length>512*1024) {//0.5M-1M
data=UIImageJPEGRepresentation(myimage, 0.5);
}else if (data.length>200*1024) {//0.25M-0.5M
data=UIImageJPEGRepresentation(myimage, 0.9);
}
}
return data;
}