详解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开发有所帮助。
您可能感兴趣的文章:
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用开发中AFNetworking库的常用HTTP操作方法小结
- iOS应用中UISearchDisplayController搜索效果的用法
- iOS App开发中的UISegmentedControl分段组件用法总结
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- iOS中UIAlertView警告框组件的使用教程
- 浅析iOS应用开发中线程间的通信与线程安全问题
- iOS中的UIKeyboard键盘视图使用方法小结
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法