详解IOS图片压缩处理

2025-05-29 0 92

前言

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

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

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

2、图片压的处理

对于“压”的功能,我们可以使用UIImageJPEGRepresentationUIImagePNGRepresentation方法实现,

如代码:

?

1

2

3

4

5

6

7
//图片压

- (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)]可以进行图片“缩”的功能。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54
/**

* 图片压缩到指定大小

* @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开发有所帮助。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 详解IOS图片压缩处理 https://www.kuaiidc.com/93071.html

相关文章

发表评论
暂无评论