使用libjpeg.framework压缩UIImage
+(void)writeFile:(NSString *)filePath withQuality:(int)quality
{ //初始化图片参数
UIImage *image=[UIImage imageNamed:@"testimg.bmp"];
JSAMPLE *image_buffer = (JSAMPLE *)[self RGBDataForImage:image];
int image_width = image.size.width;
int image_height= image.size.height;
int image_components=;
//输出图片参数
const char * filename=[filePath UTF8String];
/* This struct contains the JPEG compression parameters and pointers to
* working space (which is allocated as needed by the JPEG library).
* It is possible to have several such structures, representing multiple
* compression/decompression processes, in existence at once. We refer
* to any one struct (and its associated working data) as a "JPEG object".
*/
struct jpeg_compress_struct cinfo;
/* This struct represents a JPEG error handler. It is declared separately
* because applications often want to supply a specialized error handler
* (see the second half of this file for an example). But here we just
* take the easy way out and use the standard error handler, which will
* print a message on stderr and call exit() if compression fails.
* Note that this struct must live as long as the main JPEG parameter
* struct, to avoid dangling-pointer problems.
*/
struct jpeg_error_mgr jerr;
/* More stuff */
FILE * outfile; /* target file */
JSAMPROW row_pointer[]; /* pointer to JSAMPLE row[s] */
int row_stride; /* physical row width in image buffer */ /* Step 1: allocate and initialize JPEG compression object */ /* We have to set up the error handler first, in case the initialization
* step fails. (Unlikely, but it could happen if you are out of memory.)
* This routine fills in the contents of struct jerr, and returns jerr's
* address which we place into the link field in cinfo.
*/
cinfo.err = jpeg_std_error(&jerr);
/* Now we can initialize the JPEG compression object. */
jpeg_create_compress(&cinfo); /* Step 2: specify data destination (eg, a file) */
/* Note: steps 2 and 3 can be done in either order. */ /* Here we use the library-supplied code to send compressed data to a
* stdio stream. You can also write your own code to do something else.
* VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
* requires it in order to write binary files.
*/
if ((outfile = fopen(filename, "wb")) == NULL) {
fprintf(stderr, "can't open %s\n", filename);
exit();
}
jpeg_stdio_dest(&cinfo, outfile); /* Step 3: set parameters for compression */ /* First we supply a description of the input image.
* Four fields of the cinfo struct must be filled in:
*/
cinfo.image_width = image_width; /* image width and height, in pixels */
cinfo.image_height = image_height;
cinfo.input_components =image_components; /* # of color components per pixel */
cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
/* Now use the library's routine to set default compression parameters.
* (You must set at least cinfo.in_color_space before calling this,
* since the defaults depend on the source color space.)
*/
jpeg_set_defaults(&cinfo);
/* Now you can set any non-default parameters you wish to.
* Here we just illustrate the use of quality (quantization table) scaling:
*/
jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */); /* Step 4: Start compressor */ /* TRUE ensures that we will write a complete interchange-JPEG file.
* Pass TRUE unless you are very sure of what you're doing.
*/
jpeg_start_compress(&cinfo, TRUE); /* Step 5: while (scan lines remain to be written) */
/* jpeg_write_scanlines(...); */ /* Here we use the library's state variable cinfo.next_scanline as the
* loop counter, so that we don't have to keep track ourselves.
* To keep things simple, we pass one scanline per call; you can pass
* more if you wish, though.
*/
row_stride = image_width * ; /* JSAMPLEs per row in image_buffer */ while (cinfo.next_scanline < cinfo.image_height) {
/* jpeg_write_scanlines expects an array of pointers to scanlines.
* Here the array is only one element long, but you could pass
* more than one scanline at a time if that's more convenient.
*/
row_pointer[] = & image_buffer[cinfo.next_scanline * row_stride];
(void) jpeg_write_scanlines(&cinfo, row_pointer, );
} /* Step 6: Finish compression */ jpeg_finish_compress(&cinfo);
/* After finish_compress, we can close the output file. */
fclose(outfile); /* Step 7: release JPEG compression object */ /* This is an important step since it will release a good deal of memory. */
jpeg_destroy_compress(&cinfo);
}
+(unsigned char *)RGBDataForImage:(UIImage *)image
{
// Create a pixel buffer in an easy to use format
CGImageRef imageRef = [image CGImage];
int width = (int)CGImageGetWidth(imageRef);
int height = (int)CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); unsigned char *m_PixelBuf = malloc(sizeof(unsigned char) * height * width * );
unsigned char *outPixel= malloc(sizeof(unsigned char) * height * width * ); int bytesPerPixel = ;
int bytesPerRow = bytesPerPixel * width;
int bitsPerComponent = ;
CGContextRef context = CGBitmapContextCreate(m_PixelBuf, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); CGContextDrawImage(context, CGRectMake(, , width, height), imageRef);
CGContextRelease(context); for (int y=; y<height; y++)
{
for (int x=; x<width; x++)
{
int byteIndex = (bytesPerRow * y) + x * bytesPerPixel;
int outIndex=(*width*y)+x*;
outPixel[outIndex+]= m_PixelBuf[byteIndex+];
outPixel[outIndex+]= m_PixelBuf[byteIndex+];
outPixel[outIndex+]= m_PixelBuf[byteIndex+];
}
} CGColorSpaceRelease(colorSpace);
free(m_PixelBuf);
free(outPixel);
return outPixel; }
使用libjpeg.framework压缩UIImage的更多相关文章
- UIImage 和 iOS 图片压缩UIImage / UIImageVIew
UIImageView 制作气泡 stretchableImageWithLeftCapWidth http://blog.csdn.net/justinjing0612/article/detail ...
- iOS 关于监听手机截图,UIView生成UIImage, UIImage裁剪与压缩的总结
一. 关于监听手机截图 1. 背景: 发现商品的售价页总是被人转发截图,为了方便用户添加截图分享的小功能 首先要注册用户截屏操作的通知 - (void)viewDidLoad { [super vi ...
- iOS 图片大小压缩 图片尺寸处理
图片的压缩其实是俩概念,1.是 “压” 文件体积变小,但是像素数不变,长宽尺寸不变,那么质量可能下降,2.是 “缩” 文件的尺寸变小,也就是像素数减少.长宽尺寸变小,文件体积同样会减小. 这个 UII ...
- IOS_画图 图片等比压缩 IOS_UIImage
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{ // 创建一个bitmap的context // 并把它设置成为当前正在使用的co ...
- iOS 图片按比例压缩,指定大小压缩
使用系统方法UIImageJPEGRepresentation(UIimage *image,CGFloat quality)进行图片质量压缩,暂且叫参数quality为压缩比吧,取值范围为0-1. ...
- Android 中图片压缩分析(上)
作者: shawnzhao,QQ音乐技术团队一员 一.前言 在 Android 中进行图片压缩是非常常见的开发场景,主要的压缩方法有两种:其一是质量压缩,其二是下采样压缩. 前者是在不改变图片尺寸的情 ...
- UIImage常用封装
根据颜色返回图片,根据str返回颜色,压缩UIImage不大于300k .h代码: #import <Foundation/Foundation.h> @interface ImageSe ...
- Android图片编码机制深度解析(Bitmap,Skia,libJpeg)
问题 工作中遇到了Android中有关图片压缩保存的问题,发现这个问题还挺深,而且网上资料比较有限,因此自己深入研究了一下,算是把这个问题自顶至下全部搞懂了,在此记录. 相关的几个问题如下: 1.An ...
- iOS 图片剪切和压缩的几个方法
// 图片剪切 - (UIImage*)clipImageWithImage:(UIImage*)image inRect:(CGRect)rect { CGImageRef imageRef ...
随机推荐
- LightGBM中GBDT的实现
现在LightGBM开源了,这里将之前的一个文档发布出来供大家参考,帮助更快理解LightGBM的实现,整体思路应该是类似的. LightGBM优雅,快速,效果好,希望LightGBM越来越好:) L ...
- ORACLE导入导出数据dmp
imp testwms3/isc@TESTGMMC FILE=C:\ZKGL_201407012334.dmp ignore=y fromuser=GMMCZKGL touser=testwms ta ...
- collection of vim vim tutorial for beginner
在linux命令行下编辑文档 http://blog.csdn.net/niushuai666/article/details/7275406 http://linuxconfig.org/vim-t ...
- 定时脚本: 删除HDFS中的过期文件
1. 基本原理: 通过hadoop fs -ls *命令获取相关文件或目录的修改时间,然后与设定的过期时间进行比较,之后执行删除操作即可 2. 相关代码: #!/bin/bash source ~/. ...
- 【iOS 使用github上传代码】详解
[iOS 使用github上传代码]详解 一.github创建新工程 二.直接添加文件 三.通过https 和 SSH 操作两种方式上传工程 3.1https 和 SSH 的区别: 3.1.1.前者可 ...
- 一个神奇的POS -扫描 现场销售 开单打印票据 安卓物联网POS机 手持开单终端机 省时省力 高效准确!!
5寸高清彩屏,高端大气上档次,小巧轻便,独特的包胶防护,坚固耐用,外形精细,美观!与软件灵活对接,解决企业手工盘点,手工输单,库存管理等困难,提高准确率,提高工作效率!! 应用领域:适用于仓库.超市. ...
- Delphi的三目运算 ifthen 和iif
system.Math和system.StrUtils都有IfThen方法, 返回字符串和 返回 数值型 system.Math.IfThen(vehicle.MILE=0,0,StrToFloat( ...
- Java设置session超时(失效)的三种方式
1. 在web容器中设置(此处以tomcat为例) 在tomcat-6.0\conf\web.xml中设置,以下是tomcat 6.0中的默认配置: <!-- ================= ...
- 解决Ubuntu安装openssh-server依赖问题
sudo apt-get install openssh-server 提示:openssh-server : 依赖: openssh-client (= 1:6.6p1-2ubuntu1) 解决 u ...
- Ubuntu 14.04 更换阿里云源
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak #备份 sudo vim /etc/apt/sources.list #修改 sudo ...