iOS 图片压缩
来源:互联网 发布:ubuntu新建文件夹 编辑:程序博客网 时间:2024/06/07 20:12
- <span style="font-family: Arial, Helvetica, sans-serif;">
- </span>
iOS 对图片进行压缩处理
在开发中,我们通常需要进行图片上传等操作,例如:上传头像等,图像本身显示的就是很小一张图片,而我们直接拍照,或者相册选取的图片通常都比较大,这时候,我们就需要对图片做相应的处理,压缩图片,提升上传速率。
- </pre><pre name="code" class="objc"><span style="font-family: Arial, Helvetica, sans-serif;">+(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize</span>
- {
- // Create a graphics image context
- UIGraphicsBeginImageContext(newSize);
- // Tell the old image to draw in this new context, with the desired
- // new size
- [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
- // Get the new image from the context
- UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
- // End the context
- UIGraphicsEndImageContext();
- // Return the new image.
- return newImage;
- }
UIImageJPEGRepresentation函数需要两个参数:图片的引用和压缩系数.
而UIImagePNGRepresentation只需要图片引用作为参数.
通过在实际使用过程中,比较发现: UIImagePNGRepresentation(UIImage* image) 要比UIImageJPEGRepresentation(UIImage* image, 1.0) 返回的图片数据量大很多.
譬如,同样是读取摄像头拍摄的同样景色的照片, UIImagePNGRepresentation()返回的数据量大小为199K ,而 UIImageJPEGRepresentation(UIImage* image, 1.0)返回的数据量大小只为140KB,比前者少了50多KB.如果对图片的清晰度要求不高,还可以通过设置 UIImageJPEGRepresentation函数的第二个参数,大幅度降低图片数据量.
譬如,刚才拍摄的图片, 通过调用UIImageJPEGRepresentation(UIImage* image, 1.0)读取数据时,返回的数据大小为140KB,但更改压缩系数后,通过调用UIImageJPEGRepresentation(UIImage* image, 0.5)读取数据时,返回的数据大小只有11KB多,大大压缩了图片的数据量 ,而且从视角角度看,图片的质量并没有明显的降低.因此,在读取图片数据内容时,建议优先使用UIImageJPEGRepresentation,并可根据自己的实际使用场景,设置压缩系数,进一步降低图片数据量大小
0 0
- IOS 图片压缩
- ios--图片压缩/缩放
- IOS 图片压缩
- ios图片压缩
- IOS 图片压缩
- iOS 图片压缩
- IOS 图片压缩
- ios 图片压缩
- ios 图片压缩
- ios 图片压缩
- ios 图片压缩
- ios图片压缩
- ios 图片压缩
- iOS 图片压缩
- IOS 图片压缩
- 图片压缩 ios 上传
- ios 压缩图片
- iOS 图片压缩
- SpringMVC(四)
- virtualenv--python虚拟沙盒的安装使用
- import matplotlib出错
- "数据结构翻转课堂"答疑实录——概述
- [uva 12412]A Typical Homework(师兄帮帮忙)
- iOS 图片压缩
- Vim命令使用方法
- SpringMVC(五)
- 本地存储(数据持久化)
- VC6:Could not execute: Access denied(Win32 error 5)
- Eclipse插件之jQueryWTP插件的安装方法
- 在HTML中使用JavaScript
- Java小技巧
- [总结]视音频编解码技术零基础学习方法- 理论