前言
1、确图片的压缩的概念:
“压” 是指文件体积变小,但是像素数不变,长宽尺寸不变,那么质量可能下降。
“缩” 是指文件的尺寸变小,也就是像素数减少,而长宽尺寸变小,文件体积同样会减小。
2、图片压的处理
	对于“压”的功能,我们可以使用UIImageJPEGRepresentation或UIImagePNGRepresentation方法实现,
如代码:
| 
								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 heightelsescaleFactor = heightFactor; // scale to fit widthscaledWidth= width * scaleFactor;scaledHeight = height * scaleFactor;// center the imageif(widthFactor > heightFactor){thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;}elseif(widthFactor < heightFactor){thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;}}UIGraphicsBeginImageContext(targetSize); // this will cropCGRect 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 defaultUIGraphicsEndImageContext();returnnewImage;} | 
	这个UIImageJPEGRepresentation(image, 0.0),和 UIImagePNGRepresentation(image); 是1的功能。
	这个 [sourceImage drawInRect:CGRectMake(0,0,targetWidth, targetHeight)] 是2的功能。
总结
	所以,这俩得结合使用来满足需求,不然你一味的用1,导致,图片模糊的不行,但是尺寸还是很大。
	以上就是在IOS中压缩图片处理的详细介绍及实例,希望对大家学习IOS开发有所帮助。
相关文章
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
- 
            2025-06-04 96
- 
            2025-06-04 80
- 
            2025-05-27 102
- 
            2025-05-29 91
- 
            2025-06-04 39
 
        
 
    		 
            	 
															 
         
        
 
                        