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

详解IOS图片压缩处理

2016-07-28 17:05 706 查看

 1、确图片的压缩的概念:

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

“缩” 是指文件的尺寸变小,也就是像素数减少,而长宽尺寸变小,文件体积同样会减小。

 

 2、图片压的处理

对于“压”的功能,我们可以使用

UIImageJPEGRepresentation
UIImagePNGRepresentation
方法实现,

如代码:

//图片压
- (void)_imageCompression{
UIImage *image = [UIImage imageNamed:@"HD"];
//第一个参数是图片对象,第二个参数是压的系数,其值范围为0~1。
NSData * imageData = UIImageJPEGRepresentation(image, 0.7);
UIImage * newImage = [UIImage imageWithData:imageData];
}

2.1关于PNG和JPEG格式压缩

UIImageJPEGRepresentation
函数需要两个参数:图片的引用和压缩系数而
UIImagePNGRepresentation
只需要图片引用作为参数.

UIImagePNGRepresentation(UIImage *image)
要比
UIImageJPEGRepresentation(UIImage* image, 1.0)
返回的图片数据量大很多.

同样的一张照片, 使用

UIImagePNGRepresentation(image)
返回的数据量大小为200K,而
UIImageJPEGRepresentation(image, 1.0)
返回的数据量大小只为150K,比前者少了50K.

如果对图片的清晰度要求不是极高,建议使用

UIImageJPEGRepresentation
,可以大幅度降低图片数据量.比如,刚才拍摄的图片,通过调用
UIImageJPEGRepresentation(image, 1.0)
读取数据时,返回的数据大小为140K,但更改压缩系数为0.5再读取数据时,返回的数据大小只有11K,大大压缩了图片的数据量,而且清晰度并没有相差多少,图片的质量并没有明显的降低。因此,在读取图片数据内容时,建议优先使用
UIImageJPEGRepresentation,
并可根据自己的实际使用场景,设置压缩系数,进一步降低图片数据量大小。

提示:压缩系数不宜太低,通常是0.3~0.7,过小则可能会出现黑边等。

3、图片“缩”处理

通过

[image drawInRect:CGRectMake(0, 0, targetWidth, targetHeight)]
可以进行图片“缩”的功能。

 

/**
* 图片压缩到指定大小
* @param targetSize 目标图片的大小
* @param sourceImage 源图片
* @return 目标图片
*/
- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize withSourceImage:(UIImage *)sourceImage
{
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;
}

这个

UIImageJPEGRepresentation(image, 0.0),
UIImagePNGRepresentation(image);
是1的功能。
这个
[sourceImage drawInRect:CGRectMake(0,0,targetWidth, targetHeight)]
是2的功能。

总结

所以,这俩得结合使用来满足需求,不然你一味的用1,导致,图片模糊的不行,但是尺寸还是很大。
以上就是在IOS中压缩图片处理的详细介绍及实例,希望对大家学习IOS开发有所帮助。

您可能感兴趣的文章:

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