图片压缩
//==========================
// 图像压缩
//==========================
- (UIImage *)scaleFromImage:(UIImage *)image
{
if (!image)
{
return nil;
}
NSData *data = UIImagePNGRepresentation(image);
CGFloat dataSize = data.length/1024;
CGFloat width = image.size.width;
CGFloat height = image.size.height;
CGSize size;
if (dataSize<=50) //小于50k
{
return image;
}
else if (dataSize <= 100) //小于100k
{
size = CGSizeMake(width/1.f, height/1.f);
}
else if (dataSize <= 200) //小于200k
{
size = CGSizeMake(width/2.f, height/2.f);
}
else if (dataSize <= 500) //小于500k
{
size = CGSizeMake(width/2.f, height/2.f);
}
else if (dataSize <= 1000)//小于1M
{
size = CGSizeMake(width/4.f, height/4.f);
}
else if (dataSize <= 2000) //小于2M
{
size = CGSizeMake(width/4.f, height/4.f);
}
else//大于2M
{
size = CGSizeMake(width/4.f, height/4.f);
}
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0,0, size.width, size.height)];
UIImage *newImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if (!newImage)
{
return image;
}
return newImage;
}
//新图片的大小 = 原图/ (x.f * x.f )