iOS程序猿之图片压缩
2016-02-26 14:49
453 查看
iOS图片压缩
在开发中,我们经常会遇到需要上传图片到服务器上,如果图片过大就会消耗大量的时间,对于图片大小的处理分两个方面,一个是图片尺寸,一个是图片的所占内存的大小- (NSData *)compressedImageDataWithImage:(UIImage *)img { CGFloat compressFraction = 0.8; // 尺寸压缩比例 CGSize imageSize = img.size; CGFloat newWidth; CGFloat newHeight; if (imageSize.height > IMAGE_MAX_HEIGHT) { compressFraction = compressFraction*IMAGE_MAX_HEIGHT/imageSize.height; } if (imageSize.width*compressFraction > IMAGE_MAX_WIDTH) { compressFraction = compressFraction*IMAGE_MAX_WIDTH/imageSize.width; } newHeight = (imageSize.height)*compressFraction; newWidth = (imageSize.width)*compressFraction; CGSize newSize = CGSizeMake(newWidth, newHeight); // Create a graphics image context // 创建图片上下文 UIGraphicsBeginImageContext(newSize); // Tell the old image to draw in this new context, with the desired // new size 根据新大小重新绘制图片 [img drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; // Get the new image from the context UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); // End the context UIGraphicsEndImageContext(); // 压缩图片的大小 return UIImageJPEGRepresentation(newImage, 0.7); // 0.7是内存压缩比例 }
相关文章推荐
- IOS运行时传递对象或者添加属性
- 终端下开发iOS
- iOS开发之单例模式
- BDA -- BIOS Data Area
- ios7中的一些新特性
- iOS 设计模式 - 装饰模式
- iOS 中 cell和 label 的自适应高度
- SMB、CIFS和NETBios协议概念
- iOS开发拓展篇-XMPP简单介绍
- ios - 图片处理
- iOS的开源项目
- iOS NSFileHandle 基本用法介绍
- iOS 中有用的开源库
- iOS MTA的SDK插入,以及基本功能实现
- iOS开发,定位当前所在地理位置偏移,坐标转换
- 用CGContextRef一些画图画线的方法
- iOS开发代码如何跳转到系统的设置的无线局域网位置。
- iOS开发代码如何跳转到系统的设置的无线局域网位置。
- ioS开发知识(三十)
- iOS推送之本地推送