您的位置:首页 > 移动开发 > IOS开发

ios 对图片进行压缩

2017-06-09 16:10 253 查看
https://segmentfault.com/q/1010000000701850

我是用这个函数对图片进行压缩的
NSData *fData = UIImageJPEGRepresentation(self.photo, 1.0);

这样,图片是7MB这样。假如压缩级别是0.5,如:
NSData *fData = UIImageJPEGRepresentation(self.photo, 0.5);

图片压缩之后,大小是 1MB左右。。现在问题来了。
一张图片,压缩级别是0.5的话,大小不会变成原来的0.5倍。我算了一下,大概是0.14.所以压缩级别和这个大小好像不能形成一定的关联。
比如我一张 500KB的图片,0.5的压缩级别。大小可能才100多KB。对我来说,正好。但是假如像上面那样,7MB的大小,按照这样的压缩率,就是1MB.所以图片时大时小。对用户来说,体验肯定比较差。
我想动态的压缩图片,让没有wifi的情况下,无论多少大小的图片,压缩之后大小最大 200KB。有wifi,压缩大小最大 700KB。
目前有比较好的算法吗?或者开源的函数。
--------------------------
贴一个相关函数
//图片压缩到指定大小
- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize
{
UIImage *sourceImage = self;
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);

if (CGSizeEqualToSize(imageSize, targetSize) == NO)
{
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;

if (widthFactor > heightFactor)
scaleFactor = widthFactor; // scale to fit height
else
scaleFactor = heightFactor; // scale to fit width
scaledWidth= width * scaleFactor;
scaledHeight = height * scaleFactor;

// center the image
if (widthFactor > heightFactor)
{
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
}
else if (widthFactor < heightFactor)
{
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}

UIGraphicsBeginImageContext(targetSize); // this will crop

CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width= scaledWidth;
thumbnailRect.size.height = scaledHeight;

[sourceImage drawInRect:thumbnailRect];

newImage = UIGraphicsGetImageFromCurrentImageContext();
if(newImage == nil)
NSLog(@"could not scale image");

//pop the context to get back to the default
UIGraphicsEndImageContext();
return newImage;
}


答案对人有帮助,有参考价值
1
答案没帮助,是错误的答案,答非所问

第二个参数是压缩系数,他的设置并不能保证图片的大小,因为压缩之后的大小和图片的内容也有关系,比如你的图片的的颜色相似的话压缩之后图片的大小就会小点。你所在在意的wifi情况压缩图片是不是指需要上传呢,如果是需要上传的话你可以压缩两种格式的图片,高质量的和低质量的图片,有wifi的情况下上传高质量的图片,没有wifi的情况下上传低质量的图片

答案对人有帮助,有参考价值
1
答案没帮助,是错误的答案,答非所问

UIImageJPEGRepresentation(self.photo,
0.5);


对于每张图片进行压缩,其实有一个最小值,此后无论再怎么改小压缩系数都无济于事。

如果还是过大,只能裁剪图片了。
- (NSData *)imageWithImage:(UIImage*)image
scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return UIImageJPEGRepresentation(newImage, 0.8);
}

你可以弄一个 for 循环,不断的逼近你要的大小

答案对人有帮助,有参考价值
1
答案没帮助,是错误的答案,答非所问

- (UIImage *)compressImage:(UIImage *)image toMaxFileSize:(NSInteger)maxFileSize {
CGFloat compression = 0.9f;
CGFloat maxCompression = 0.1f;
NSData *imageData = UIImageJPEGRepresentation(image, compression);
while ([imageData length] > maxFileSize && compression > maxCompression) {
compression -= 0.1;
imageData = UIImageJPEGRepresentation(image, compression);
}

UIImage *compressedImage = [UIImage imageWithData:imageData];
return compressedImage;
}


图片的压缩其实是俩概念,

1、是 “压” 文件体积变小,但是像素数不变,长宽尺寸不变,那么质量可能下降,

2、是 “缩” 文件的尺寸变小,也就是像素数减少。长宽尺寸变小,文件体积同样会减小。
这个 UIImageJPEGRepresentation(image, 0.0),是1的功能。这个 [sourceImage drawInRect:CGRectMake(0,0,targetWidth, targetHeight)] 是2的功能。
所以,这俩你得结合使用来满足需求,不然你一味的用1,导致,图片模糊的不行,但是尺寸还是很大。

答案对人有帮助,有参考价值
0
答案没帮助,是错误的答案,答非所问

我有个图片,可能是14M或者更多,想把它压缩到500K以内,UIImageJPEGRepresentation(image, 0.0),就算这个压缩系数设置为最小,压缩后仍然比500k大;那有什么方法能把任意大小的图片压缩到指定的字节大小呢,忽略图片清晰度的要求!楼主解决此问题没,求解!



//图片压缩到指定大小

(UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize

{

UIImage *sourceImage = self;

UIImage *newImage = nil;

CGSize imageSize = sourceImage.size;

CGFloat width = imageSize.width;

CGFloat height = imageSize.height;

CGFloat targetWidth = targetSize.width;

CGFloat targetHeight = targetSize.height;

CGFloat scaleFactor = 0.0;

CGFloat scaledWidth = targetWidth;

CGFloat scaledHeight = targetHeight;

CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;

if (widthFactor > heightFactor)
scaleFactor = widthFactor; // scale to fit height
else
scaleFactor = heightFactor; // scale to fit width
scaledWidth= width * scaleFactor;
scaledHeight = height * scaleFactor;

// center the image
if (widthFactor > heightFactor)
{
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
}
else if (widthFactor < heightFactor)
{
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}

}
UIGraphicsBeginImageContext(targetSize); // this will crop
CGRect thumbnailRect = CGRectZero;

thumbnailRect.origin = thumbnailPoint;

thumbnailRect.size.width= scaledWidth;

thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();

if(newImage == nil)

NSLog(@"could not scale image");
//pop the context to get back to the default

UIGraphicsEndImageContext();

return newImage;

}

— Milu ·
2015年12月25日

展开评论

答案对人有帮助,有参考价值
0
答案没帮助,是错误的答案,答非所问

-(UIImage *) imageCompressForWidth:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth
{
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = defineWidth;
CGFloat targetHeight = (targetWidth / width) * height;
UIGraphicsBeginImageContext(CGSizeMake(targetWidth, targetHeight));
[sourceImage drawInRect:CGRectMake(0,0,targetWidth,  targetHeight)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}


答案对人有帮助,有参考价值
0
答案没帮助,是错误的答案,答非所问

压图片质量和缩小图片大小相结合:

/**
压图片质量

@param image image
@return Data
*/
+ (NSData *)zipImageWithImage:(UIImage *)image
{
if (!image) {
return nil;
}
CGFloat maxFileSize = 32*1024;
CGFloat compression = 0.9f;
NSData *compressedData = UIImageJPEGRepresentation(image, compression);
while ([compressedData length] > maxFileSize) {
compression *= 0.9;
compressedData = UIImageJPEGRepresentation([[self class] compressImage:image newWidth:image.size.width*compression], compression);
}
return compressedData;
}

/**
*  等比缩放本图片大小
*
*  @param newImageWidth 缩放后图片宽度,像素为单位
*
*  @return self-->(image)
*/
+ (UIImage *)compressImage:(UIImage *)image newWidth:(CGFloat)newImageWidth
{
if (!image) return nil;
float imageWidth = image.size.width;
float imageHeight = image.size.height;
float width = newImageWidth;
float height = image.size.height/(image.size.width/width);

float widthScale = imageWidth /width;
float heightScale = imageHeight /height;

// 创建一个bitmap的context
// 并把它设置成为当前正在使用的context
UIGraphicsBeginImageContext(CGSizeMake(width, height));

if (widthScale > heightScale) {
[image drawInRect:CGRectMake(0, 0, imageWidth /heightScale , height)];
}
else {
[image drawInRect:CGRectMake(0, 0, width , imageHeight /widthScale)];
}

// 从当前context中创建一个改变大小后的图片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
// 使当前的context出堆栈
UIGraphicsEndImageContext();

return newImage;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: