图片压缩
Posted 阳光普照大地
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图片压缩相关的知识,希望对你有一定的参考价值。
//==========================
// 图像压缩
//==========================
- (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 )
以上是关于图片压缩的主要内容,如果未能解决你的问题,请参考以下文章